summaryrefslogtreecommitdiffstats
path: root/src/plugins/ifw/report_list.c
blob: c21df7eed637a75e05b7d23e7697a800f5b8bfd1 (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
77
78
79
80
81
#include "report_list.h"

#include <stdio.h>
#include <stdlib.h>

void report_list_init(report_list_t *list) {
    INIT_LIST_HEAD(list);
}

report_list_cell_t *report_list_add(report_list_t *list, int seq, msg_usr_t *attack) {
    report_list_cell_t *cell;

    cell = malloc(sizeof(report_list_cell_t));
    if (!cell) {
        fprintf(stderr, "unable to alloc enough memory for report list cell, skipping\n");
        return NULL;
    }
    cell->seq = seq;
    cell->info = *attack;
    cell->processed = 0;
    INIT_LIST_HEAD(&cell->list);
    list_add_tail(&cell->list, list);

    return cell;
}

report_list_cell_t *report_list_find(report_list_t *list, u_int32_t addr, int include_processed) {
    struct list_head *entry;

    __list_for_each(entry, list) {
        report_list_cell_t *cell;
        cell = list_entry(entry, report_list_cell_t, list);
        if (cell->info.s_addr == addr && include_processed || !cell->processed) {
            return cell;
        }
    }

    return NULL;
}

report_list_cell_t *report_list_find_seq(report_list_t *list, int seq) {
    struct list_head *entry;

    __list_for_each(entry, list) {
        report_list_cell_t *cell;
        cell = list_entry(entry, report_list_cell_t, list);
        if (cell->seq == seq) {
            return cell;
        }
    }

    return NULL;
}

void report_list_remove(report_list_cell_t *cell) {
    list_del(&cell->list);
    free(cell);
}

void report_list_clear_processed(report_list_t *list) {
    report_list_cell_t *cell, *n;

    list_for_each_entry_safe(cell, n, list, list) {
        if (cell->processed) {
            report_list_remove(cell);
        }
    }
}

void report_list_print(report_list_t *list) {
    struct list_head *entry;

    printf("* report list {\n");
    __list_for_each(entry, list) {
        report_list_cell_t *cell;
        cell = list_entry(entry, report_list_cell_t, list);
        printf("%d,\n", cell->seq);
    }
    printf("} report list *\n");
}