summaryrefslogtreecommitdiffstats
path: root/src/plugins/ifw/black_list.c
blob: 1e7dbbb6f8da57013655baa7b40b8de8db8f1233 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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");
}