Browse Source

* unify f2b_filter_cmd_*()

master
Alex 'AdUser' Z 8 years ago
parent
commit
34816f9298
  1. 5
      src/daemon.c
  2. 2
      src/filter-test.c
  3. 37
      src/filter.c
  4. 4
      src/filter.h

5
src/daemon.c

@ -135,10 +135,9 @@ f2b_cmsg_process(const f2b_cmsg_t *msg, char *res, size_t ressize) {
} else if (msg->type == CMD_JAIL_IP_RELEASE) {
f2b_jail_cmd_ip_xxx(res, ressize, jail, -1, args[1]);
} else if (msg->type == CMD_JAIL_FILTER_STATS) {
f2b_filter_stats(jail->filter, res, ressize);
f2b_filter_cmd_stats(res, ressize, jail->filter);
} else if (msg->type == CMD_JAIL_FILTER_RELOAD) {
if (f2b_filter_reload(jail->filter) == false)
strlcpy(res, f2b_filter_error(jail->filter), ressize);
f2b_filter_cmd_reload(res, ressize, jail->filter);
} else {
strlcpy(res, "error: unsupported command type", ressize);
}

2
src/filter-test.c

@ -58,7 +58,7 @@ int main(int argc, char *argv[]) {
}
fclose(file);
fprintf(stderr, "stats: %% lines read: %zu, matched: %zu\n", read, matched);
f2b_filter_stats(filter, stats, sizeof(stats));
f2b_filter_cmd_stats(stats, sizeof(stats), filter);
fputs(stats, stderr);
return EXIT_SUCCESS;

37
src/filter.c

@ -175,14 +175,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);
}
bool
f2b_filter_reload(f2b_filter_t *filter) {
assert(filter != NULL);
filter->flush(filter->cfg);
return f2b_filter_load_file(filter, filter->file);
}
const char *
f2b_filter_error(f2b_filter_t *filter) {
assert(filter != NULL);
@ -190,20 +182,33 @@ f2b_filter_error(f2b_filter_t *filter) {
}
void
f2b_filter_stats(f2b_filter_t *filter, char *res, size_t ressize) {
assert(filter != NULL);
assert(res != NULL);
f2b_filter_cmd_stats(char *buf, size_t bufsize, f2b_filter_t *filter) {
bool reset = true;
char *pattern;
int matches;
char buf[256];
char tmp[256];
const char *fmt =
"- pattern: %s\n"
" matches: %d\n";
res[0] = '\0';
int matches;
assert(filter != NULL);
assert(buf != NULL);
buf[0] = '\0';
while (filter->stats(filter->cfg, &matches, &pattern, reset)) {
snprintf(buf, sizeof(buf), fmt, pattern, matches);
strlcat(res, buf, ressize);
snprintf(tmp, sizeof(tmp), fmt, pattern, matches);
strlcat(buf, tmp, bufsize);
reset = false;
}
}
void
f2b_filter_cmd_reload(char *buf, size_t bufsize, f2b_filter_t *filter) {
assert(buf != NULL);
assert(filter != NULL);
filter->flush(filter->cfg);
if (f2b_filter_load_file(filter, filter->file))
return;
strlcpy(buf, f2b_filter_error(filter), bufsize);
}

4
src/filter.h

@ -27,10 +27,10 @@ typedef struct f2b_filter_t {
f2b_filter_t * f2b_filter_create (f2b_config_section_t *config, const char *id);
const char * f2b_filter_error (f2b_filter_t *f);
bool f2b_filter_reload (f2b_filter_t *f);
bool f2b_filter_append (f2b_filter_t *f, const char *pattern);
bool f2b_filter_match (f2b_filter_t *f, const char *line, char *buf, size_t buf_size);
void f2b_filter_stats (f2b_filter_t *f, char *res, size_t ressize);
void f2b_filter_destroy (f2b_filter_t *f);
void f2b_filter_cmd_reload(char *buf, size_t bufsize, f2b_filter_t *f);
void f2b_filter_cmd_stats (char *buf, size_t bufsize, f2b_filter_t *f);
#endif /* F2B_FILTER_H_ */

Loading…
Cancel
Save