summaryrefslogtreecommitdiffstats
path: root/src/plugins/ifw/black_list.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/ifw/black_list.c')
-rw-r--r--src/plugins/ifw/black_list.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/plugins/ifw/black_list.c b/src/plugins/ifw/black_list.c
new file mode 100644
index 0000000..1e7dbbb
--- /dev/null
+++ b/src/plugins/ifw/black_list.c
@@ -0,0 +1,76 @@
+#include "black_list.h"
+#include "ipset.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <sys/socket.h>
+
+void black_list_init(black_list_t *list) {
+ INIT_LIST_HEAD(list);
+}
+
+void black_list_add(black_list_t *list, msg_usr_t *attack) {
+ black_list_cell_t *cell;
+
+ cell = malloc(sizeof(black_list_cell_t));
+ if (!cell) {
+ fprintf(stderr, "unable to alloc enough memory for black list cell, skipping\n");
+ return;
+ }
+ cell->info = *attack;
+ INIT_LIST_HEAD(&cell->list);
+ list_add_tail(&cell->list, list);
+
+ ipset_blacklist_add(cell->info.s_addr);
+}
+
+black_list_cell_t *black_list_find(black_list_t *list, u_int32_t addr) {
+ struct list_head *entry;
+
+ __list_for_each(entry, list) {
+ black_list_cell_t *cell;
+ cell = list_entry(entry, black_list_cell_t, list);
+ if (cell->info.s_addr == addr) {
+ return cell;
+ }
+ }
+
+ return NULL;
+}
+
+void black_list_remove(black_list_t *list, u_int32_t addr) {
+ black_list_cell_t *cell, *n, *prev;
+
+ ipset_blacklist_remove(addr);
+
+ prev = NULL;
+ list_for_each_entry_safe(cell, n, list, list) {
+ if (prev)
+ free(prev);
+ if (cell->info.s_addr == addr) {
+ list_del(&cell->list);
+ prev = cell;
+ } else {
+ prev = NULL;
+ }
+ }
+ if (prev)
+ free(prev);
+}
+
+
+void black_list_print(black_list_t *list) {
+ struct list_head *entry;
+
+ printf("* black list {\n");
+ __list_for_each(entry, list) {
+ black_list_cell_t *cell;
+ struct in_addr addr;
+ cell = list_entry(entry, black_list_cell_t, list);
+ addr.s_addr = cell->info.s_addr;
+ printf("%s,\n", inet_ntoa(addr));
+ }
+ printf("} black list *\n");
+}