Browse Source

* fix filter-test

master
Alex 'AdUser' Z 9 years ago
parent
commit
9ae7a8e2e5
  1. 7
      src/CMakeLists.txt
  2. 36
      src/filter-test.c

7
src/CMakeLists.txt

@ -5,12 +5,13 @@ set(SOURCES "main.c" "logfile.c" "log.c" "matches.c" "ipaddr.c" "filelist.c" "fi
add_executable("f2b" ${SOURCES}) add_executable("f2b" ${SOURCES})
target_link_libraries(f2b "dl") target_link_libraries(f2b "dl")
set(SOURCES "backend-test.c" "log.c" "backend.c" "config.c") set(SOURCES "backend-test.c" "log.c" "config.c" "backend.c")
add_executable("backend-test" ${SOURCES}) add_executable("backend-test" ${SOURCES})
target_link_libraries("backend-test" "dl") target_link_libraries("backend-test" "dl")
#set(SOURCES "filter-test.c" "log.c" "filter.c") set(SOURCES "filter-test.c" "log.c" "config.c" "filter.c")
#add_executable("filter-test" ${SOURCES}) add_executable("filter-test" ${SOURCES})
target_link_libraries("filter-test" "dl")
install(TARGETS f2b install(TARGETS f2b
RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/bin") RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/bin")

36
src/filter-test.c

@ -7,43 +7,41 @@
#include "common.h" #include "common.h"
#include "log.h" #include "log.h"
#include "ipaddr.h" #include "ipaddr.h"
#include "regexps.h" #include "config.h"
#include "filter.h"
void usage() { void usage() {
fprintf(stderr, "Usage: filter-test <regexps-file.txt> <logfile.txt>\n"); fprintf(stderr, "Usage: filter-test <library.so> <regexps-file.txt>\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
FILE *f = NULL; f2b_config_param_t param = { .name = "load", .value = "", .next = 0x0 };
char matchbuf[IPADDR_MAX] = ""; f2b_config_section_t config = { .type = t_filter, .param = 0x0, .next = 0x0 };
char logline[LOGLINE_MAX] = ""; f2b_filter_t *filter = NULL;
f2b_regex_t *list = NULL; char match[IPADDR_MAX] = "";
char line[LOGLINE_MAX] = "";
size_t read = 0, matched = 0; size_t read = 0, matched = 0;
if (argc < 3) if (argc < 3)
usage(); usage();
if ((list = f2b_regexlist_from_file(argv[1])) == NULL) { config.param = &param;
f2b_log_msg(log_error, "can't load regexps list from file '%s'", argv[1]); snprintf(param.value, sizeof(param.value), "%s", argv[1]);
return EXIT_FAILURE;
}
if ((f = fopen(argv[2], "r")) == NULL) { if ((filter = f2b_filter_create(&config, argv[2])) == false)
f2b_log_msg(log_error, "can't open logfile '%s'", argv[2]); usage();
return EXIT_FAILURE;
}
while (fgets(logline, sizeof(logline), f) != NULL) { while (fgets(line, sizeof(line), stdin) != NULL) {
read++; read++;
if (f2b_regexlist_match(list, logline, matchbuf, sizeof(matchbuf))) { if (f2b_filter_match(filter, line, match, sizeof(match))) {
matched++; matched++;
f2b_log_msg(log_info, "match found: %s", matchbuf); fprintf(stderr, "+ %s\n", match);
continue; continue;
} }
f2b_log_msg(log_info, "unmatched line: %s", logline); fprintf(stderr, "- (no-match): %s", line);
} }
f2b_log_msg(log_info, "lines read: %d, matched: %d", read, matched); fprintf(stderr, "%% lines read: %d, matched: %d\n", read, matched);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

Loading…
Cancel
Save