forked from ntop/ipt_geofence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlacklists.cpp
203 lines (152 loc) · 4.88 KB
/
Blacklists.cpp
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
*
* (C) 2021-22 - ntop.org
*
*
* 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 3 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 "include.h"
/* ****************************************** */
Blacklists::Blacklists() {
ptree_v4 = ndpi_patricia_new(32);
ptree_v6 = ndpi_patricia_new(128);
}
/* ****************************************** */
static void free_ptree_data(void *data) {
if(data) free(data);
}
/* ****************************************** */
Blacklists::~Blacklists() {
if(ptree_v4)
ndpi_patricia_destroy(ptree_v4, free_ptree_data);
if(ptree_v6)
ndpi_patricia_destroy(ptree_v6, free_ptree_data);
}
/* ****************************************** */
void Blacklists::addAddress(int family, void *addr, int bits) {
ndpi_prefix_t prefix;
ndpi_patricia_node_t *node;
ndpi_patricia_tree_t *tree;
if (family == AF_INET)
tree = ptree_v4, ndpi_fill_prefix_v4(&prefix, (struct in_addr *) addr, bits, ptree_v4->maxbits);
else
tree = ptree_v6, ndpi_fill_prefix_v6(&prefix, (struct in6_addr *) addr, bits, ptree_v6->maxbits);
if((node = ndpi_patricia_lookup(tree, &prefix)) != NULL)
ndpi_patricia_set_node_data(node, NULL);
}
/* ****************************************** */
bool Blacklists::isBlacklistedIPv4(struct in_addr *addr) {
ndpi_prefix_t prefix;
ndpi_fill_prefix_v4(&prefix, addr, 32, ptree_v4->maxbits);
if(ndpi_patricia_search_best(ptree_v4, &prefix) != NULL)
return(true);
else
return(false);
}
/* ****************************************** */
bool Blacklists::isBlacklistedIPv6(struct in6_addr *addr6) {
ndpi_prefix_t prefix;
ndpi_fill_prefix_v6(&prefix, addr6, 128, ptree_v6->maxbits);
if(ndpi_patricia_search_best(ptree_v6, &prefix) != NULL)
return(true);
else
return(false);
}
/* ****************************************** */
bool Blacklists::findAddress(char *addr) {
ndpi_prefix_t prefix;
ndpi_patricia_node_t *node;
ndpi_patricia_tree_t *tree;
if(strchr(addr, ':') != NULL) {
struct in6_addr addr6;
if(inet_pton(AF_INET6, addr, &addr6))
return(isBlacklistedIPv6(&addr6));
else
return(false); /* Conversion failed */
} else {
struct in_addr pin;
inet_aton(addr, &pin);
return(isBlacklistedIPv4(&pin));
}
}
/* ****************************************** */
void Blacklists::addAddress(char *net) {
char *_bits = strchr(net, '/');
u_int bits = 0;
if(_bits)
bits = atoi(&_bits[1]), _bits[0] = '\0';
if(strchr(net, ':') != NULL) {
struct in6_addr addr6;
if(bits == 0) bits = 128;
if(inet_pton(AF_INET6, net, &addr6))
addAddress(AF_INET6, &addr6, bits);
} else {
struct in_addr pin;
if(bits == 0) bits = 32;
inet_aton(net, &pin);
addAddress(AF_INET, &pin, bits);
}
}
/* ****************************************** */
bool Blacklists::loadIPsetFromFile(const char *path) {
std::ifstream infile(path);
std::string line;
if(!infile.is_open())
return(false);
while(getline(infile, line)){
if((line[0] == '#') || (line[0] == '\0'))
continue;
// trace->traceEvent(TRACE_INFO, "Adding %s", line.c_str());
addAddress((char*)line.c_str());
}
infile.close();
return(true);
}
/* ****************************************** */
bool Blacklists::loadIPsetFromURL(const char *url) {
CURL *curl = curl_easy_init();
FILE *fd;
CURLcode res;
char tmp_filename[64] = "/tmp/ipset_tempfile-XXXXXX";
bool rc;
if(!curl) {
trace->traceEvent(TRACE_ERROR, "Unable to init curl");
return(false);
}
if((fd = fopen(tmp_filename, "w")) == NULL) {
trace->traceEvent(TRACE_ERROR, "Unable to open temporary file %s", tmp_filename);
return(false);
}
trace->traceEvent(TRACE_NORMAL, "Downloading %s...", url);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fd);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fd);
if(res == CURLE_OK) {
rc = loadIPsetFromFile(tmp_filename);
} else {
trace->traceEvent(TRACE_ERROR, "Error while downloading %s", url);
rc = false;
}
unlink(tmp_filename); // Delete temporary file
#ifdef TEST
trace->traceEvent(TRACE_WARNING, "=>> %s",
findAddress((char*)"2.57.121.1") ? "DROP" : "PASS");
#endif
return(rc);
}