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
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(buf != NULL);
buf[0] = '\0';
while (filter->stats(filter->cfg, &matches, &pattern, reset)) {
snprintf(tmp, sizeof(tmp), fmt, pattern, matches);
strlcat(buf, tmp, bufsize);
reset = false;
}
filter->stats(filter->cfg, buf, bufsize);
}
void

2
src/filter.h

@ -34,7 +34,7 @@ typedef struct f2b_filter_t {
/** dlsym pointer to handler of @a flush command */
bool (*flush) (void *cfg);
/** 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 */
bool (*match) (void *cfg, const char *line, char *buf, size_t buf_size);
/** 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;
}
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
*/
typedef struct _config cfg_t;
typedef struct _regexp rx_t;
/**
* @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));
/**
* @brief Fetch match stats by-pattern
* @brief Fetch filter match stats
* @param cfg Module handler
* @param matches Pointer to storage for matches count
* @param pattern Associated pattern
* @param reset Reset to start of statistics (for later calls)
* @returns false on no more stats or true otherwise with filling @a matches and @a pattern
* @param buf Pointer to storage for stats report
* @param bufsize Available size for report on @a buf pointer
* @returns true on success, false on error
*/
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
* @param cfg Module handler

37
src/filters/pcre.c

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

38
src/filters/preg.c

@ -6,25 +6,25 @@
*/
#include <regex.h>
#include "../strlcpy.h"
#include "filter.h"
#define MODNAME "preg"
/* draft */
#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 f2b_regex_t *next;
struct _regexp {
rx_t *next;
char pattern[PATTERN_MAX];
int matches;
regex_t regex;
} f2b_regex_t;
};
struct _config {
char id[ID_MAX];
bool icase;
void (*logcb)(enum loglevel lvl, const char *msg);
f2b_regex_t *regexps;
f2b_regex_t *statp;
rx_t *regexps;
};
#include "filter.c"
@ -57,7 +57,7 @@ config(cfg_t *cfg, const char *key, const char *value) {
bool
append(cfg_t *cfg, const char *pattern) {
f2b_regex_t *regex = NULL;
rx_t *regex = NULL;
int flags = REG_EXTENDED;
size_t bufsize;
char *buf = NULL;
@ -81,7 +81,7 @@ append(cfg_t *cfg, const char *pattern) {
strlcat(buf, HOST_REGEX, 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;
if ((ret = regcomp(&regex->regex, buf, flags)) == 0) {
@ -107,26 +107,8 @@ ready(cfg_t *cfg) {
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
match(cfg_t *cfg, const char *line, char *buf, size_t buf_size) {
f2b_regex_t *r = NULL;
size_t match_len = 0;
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(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)
continue;
/* matched */
@ -152,11 +134,11 @@ match(cfg_t *cfg, const char *line, char *buf, size_t buf_size) {
void
flush(cfg_t *cfg) {
f2b_regex_t *next = NULL, *r = NULL;
rx_t *next = 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;
regfree(&r->regex);
free(r);

2
src/source.h

@ -32,6 +32,8 @@ typedef struct f2b_source_t {
bool (*start) (void *cfg);
/** dlsym pointer to handler of @a next command */
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 */
bool (*stop) (void *cfg);
/** dlsym pointer to handler of @a destroy command */

Loading…
Cancel
Save