forked from DataSoft/Honeyd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.c
156 lines (131 loc) · 3.89 KB
/
filter.c
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright (c) 2003, 2004 Niels Provos <[email protected]>
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <sys/types.h>
#include <sys/param.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/stat.h>
#include <syslog.h>
#include <sys/queue.h>
#include <sys/tree.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dnet.h>
#include <event.h>
#include "tagging.h"
#include "histogram.h"
#include "analyze.h"
#include "filter.h"
/* Report filtering to select topN reports */
SPLAY_HEAD(filtertree, filter);
static int
filter_compare(struct filter *a, struct filter *b)
{
if (a->count < b->count)
return (-1);
else if (a->count > b->count)
return (1);
if (a->report < b->report)
return (-1);
else if (a->report > b->report)
return (1);
return (0);
}
SPLAY_PROTOTYPE(filtertree, filter, node, filter_compare);
SPLAY_GENERATE(filtertree, filter, node, filter_compare);
struct filtertree *
filter_create(void)
{
struct filtertree *filters;
if ((filters = calloc(1, sizeof(struct filtertree))) == NULL)
{
syslog(LOG_ERR, "%s: calloc, failed to allocate filtertree",__func__);
exit(EXIT_FAILURE);
}
SPLAY_INIT(filters);
return (filters);
}
void
filter_free(struct filtertree *filters)
{
struct filter *filter;
while ((filter = SPLAY_ROOT(filters)) != NULL) {
SPLAY_REMOVE(filtertree, filters, filter);
free(filter);
}
free(filters);
}
/*
* Inserts back reference and count into a filter tree. Unfortunately,
* this currently uses O(n log n) even though we are interested only in
* the top<K> entries.
*/
void
filter_insert(struct filtertree *filters, uint32_t count, void *report)
{
struct filter *filter, tmp;
tmp.count = count;
tmp.report = report;
/* Check if we already manage this data */
if ((filter = SPLAY_FIND(filtertree, filters, &tmp)) != NULL)
return;
if ((filter = calloc(1, sizeof(struct filter))) == NULL)
{
syslog(LOG_ERR, "%s: calloc failed to allocate filter",__func__);
exit(EXIT_FAILURE);
}
filter->count = count;
filter->report = report;
SPLAY_INSERT(filtertree, filters, filter);
}
/* Filters out the top<n> entries */
void
filter_top(struct filtertree *filters, int n, void (*cb)(void *, void *),
void *arg)
{
int i = 0;
struct filter *filter;
while (i++ < n) {
if ((filter = SPLAY_MAX(filtertree, filters)) == NULL ||
filter->count == 0)
break;
SPLAY_REMOVE(filtertree, filters, filter);
(*cb)(filter->report, arg);
free (filter);
}
}