1 | /* |
---|
2 | * This file Copyright (C) 2008-2014 Mnemosyne LLC |
---|
3 | * |
---|
4 | * It may be used under the GNU GPL versions 2 or 3 |
---|
5 | * or any future license endorsed by Mnemosyne LLC. |
---|
6 | * |
---|
7 | * $Id: blocklist.c 14327 2014-07-28 04:13:38Z jordan $ |
---|
8 | */ |
---|
9 | |
---|
10 | #include <assert.h> |
---|
11 | #include <errno.h> |
---|
12 | #include <stdio.h> |
---|
13 | #include <stdlib.h> /* bsearch (), qsort () */ |
---|
14 | #include <string.h> |
---|
15 | |
---|
16 | #include "transmission.h" |
---|
17 | #include "blocklist.h" |
---|
18 | #include "error.h" |
---|
19 | #include "file.h" |
---|
20 | #include "log.h" |
---|
21 | #include "net.h" |
---|
22 | #include "utils.h" |
---|
23 | |
---|
24 | |
---|
25 | /*** |
---|
26 | **** PRIVATE |
---|
27 | ***/ |
---|
28 | |
---|
29 | struct tr_ipv4_range |
---|
30 | { |
---|
31 | uint32_t begin; |
---|
32 | uint32_t end; |
---|
33 | }; |
---|
34 | |
---|
35 | struct tr_blocklistFile |
---|
36 | { |
---|
37 | bool isEnabled; |
---|
38 | tr_sys_file_t fd; |
---|
39 | size_t ruleCount; |
---|
40 | uint64_t byteCount; |
---|
41 | char * filename; |
---|
42 | struct tr_ipv4_range * rules; |
---|
43 | }; |
---|
44 | |
---|
45 | static void |
---|
46 | blocklistClose (tr_blocklistFile * b) |
---|
47 | { |
---|
48 | if (b->rules != NULL) |
---|
49 | { |
---|
50 | tr_sys_file_unmap (b->rules, b->byteCount, NULL); |
---|
51 | tr_sys_file_close (b->fd, NULL); |
---|
52 | b->rules = NULL; |
---|
53 | b->ruleCount = 0; |
---|
54 | b->byteCount = 0; |
---|
55 | b->fd = TR_BAD_SYS_FILE; |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | static void |
---|
60 | blocklistLoad (tr_blocklistFile * b) |
---|
61 | { |
---|
62 | tr_sys_file_t fd; |
---|
63 | uint64_t byteCount; |
---|
64 | tr_sys_path_info info; |
---|
65 | char * base; |
---|
66 | tr_error * error = NULL; |
---|
67 | const char * err_fmt = _("Couldn't read \"%1$s\": %2$s"); |
---|
68 | |
---|
69 | blocklistClose (b); |
---|
70 | |
---|
71 | if (!tr_sys_path_get_info (b->filename, 0, &info, NULL)) |
---|
72 | return; |
---|
73 | |
---|
74 | byteCount = info.size; |
---|
75 | if (byteCount == 0) |
---|
76 | return; |
---|
77 | |
---|
78 | fd = tr_sys_file_open (b->filename, TR_SYS_FILE_READ, 0, &error); |
---|
79 | if (fd == TR_BAD_SYS_FILE) |
---|
80 | { |
---|
81 | tr_logAddError (err_fmt, b->filename, error->message); |
---|
82 | tr_error_free (error); |
---|
83 | return; |
---|
84 | } |
---|
85 | |
---|
86 | b->rules = tr_sys_file_map_for_reading (fd, 0, byteCount, &error); |
---|
87 | if (!b->rules) |
---|
88 | { |
---|
89 | tr_logAddError (err_fmt, b->filename, error->message); |
---|
90 | tr_sys_file_close (fd, NULL); |
---|
91 | tr_error_free (error); |
---|
92 | return; |
---|
93 | } |
---|
94 | |
---|
95 | b->fd = fd; |
---|
96 | b->byteCount = byteCount; |
---|
97 | b->ruleCount = byteCount / sizeof (struct tr_ipv4_range); |
---|
98 | |
---|
99 | base = tr_sys_path_basename (b->filename, NULL); |
---|
100 | tr_logAddInfo (_("Blocklist \"%s\" contains %"TR_PRIuSIZE" entries"), base, b->ruleCount); |
---|
101 | tr_free (base); |
---|
102 | } |
---|
103 | |
---|
104 | static void |
---|
105 | blocklistEnsureLoaded (tr_blocklistFile * b) |
---|
106 | { |
---|
107 | if (b->rules == NULL) |
---|
108 | blocklistLoad (b); |
---|
109 | } |
---|
110 | |
---|
111 | static int |
---|
112 | compareAddressToRange (const void * va, const void * vb) |
---|
113 | { |
---|
114 | const uint32_t * a = va; |
---|
115 | const struct tr_ipv4_range * b = vb; |
---|
116 | |
---|
117 | if (*a < b->begin) return -1; |
---|
118 | if (*a > b->end) return 1; |
---|
119 | return 0; |
---|
120 | } |
---|
121 | |
---|
122 | static void |
---|
123 | blocklistDelete (tr_blocklistFile * b) |
---|
124 | { |
---|
125 | blocklistClose (b); |
---|
126 | tr_sys_path_remove (b->filename, NULL); |
---|
127 | } |
---|
128 | |
---|
129 | /*** |
---|
130 | **** PACKAGE-VISIBLE |
---|
131 | ***/ |
---|
132 | |
---|
133 | tr_blocklistFile * |
---|
134 | tr_blocklistFileNew (const char * filename, bool isEnabled) |
---|
135 | { |
---|
136 | tr_blocklistFile * b; |
---|
137 | |
---|
138 | b = tr_new0 (tr_blocklistFile, 1); |
---|
139 | b->fd = TR_BAD_SYS_FILE; |
---|
140 | b->filename = tr_strdup (filename); |
---|
141 | b->isEnabled = isEnabled; |
---|
142 | |
---|
143 | return b; |
---|
144 | } |
---|
145 | |
---|
146 | const char* |
---|
147 | tr_blocklistFileGetFilename (const tr_blocklistFile * b) |
---|
148 | { |
---|
149 | return b->filename; |
---|
150 | } |
---|
151 | |
---|
152 | void |
---|
153 | tr_blocklistFileFree (tr_blocklistFile * b) |
---|
154 | { |
---|
155 | blocklistClose (b); |
---|
156 | tr_free (b->filename); |
---|
157 | tr_free (b); |
---|
158 | } |
---|
159 | |
---|
160 | bool |
---|
161 | tr_blocklistFileExists (const tr_blocklistFile * b) |
---|
162 | { |
---|
163 | return tr_sys_path_exists (b->filename, NULL); |
---|
164 | } |
---|
165 | |
---|
166 | int |
---|
167 | tr_blocklistFileGetRuleCount (const tr_blocklistFile * b) |
---|
168 | { |
---|
169 | blocklistEnsureLoaded ((tr_blocklistFile*)b); |
---|
170 | |
---|
171 | return b->ruleCount; |
---|
172 | } |
---|
173 | |
---|
174 | bool |
---|
175 | tr_blocklistFileIsEnabled (tr_blocklistFile * b) |
---|
176 | { |
---|
177 | return b->isEnabled; |
---|
178 | } |
---|
179 | |
---|
180 | void |
---|
181 | tr_blocklistFileSetEnabled (tr_blocklistFile * b, bool isEnabled) |
---|
182 | { |
---|
183 | assert (b != NULL); |
---|
184 | assert (tr_isBool (isEnabled)); |
---|
185 | |
---|
186 | b->isEnabled = isEnabled; |
---|
187 | } |
---|
188 | |
---|
189 | bool |
---|
190 | tr_blocklistFileHasAddress (tr_blocklistFile * b, const tr_address * addr) |
---|
191 | { |
---|
192 | uint32_t needle; |
---|
193 | const struct tr_ipv4_range * range; |
---|
194 | |
---|
195 | assert (tr_address_is_valid (addr)); |
---|
196 | |
---|
197 | if (!b->isEnabled || addr->type == TR_AF_INET6) |
---|
198 | return false; |
---|
199 | |
---|
200 | blocklistEnsureLoaded (b); |
---|
201 | |
---|
202 | if (!b->rules || !b->ruleCount) |
---|
203 | return false; |
---|
204 | |
---|
205 | needle = ntohl (addr->addr.addr4.s_addr); |
---|
206 | |
---|
207 | range = bsearch (&needle, |
---|
208 | b->rules, |
---|
209 | b->ruleCount, |
---|
210 | sizeof (struct tr_ipv4_range), |
---|
211 | compareAddressToRange); |
---|
212 | |
---|
213 | return range != NULL; |
---|
214 | } |
---|
215 | |
---|
216 | /* |
---|
217 | * P2P plaintext format: "comment:x.x.x.x-y.y.y.y" |
---|
218 | * http://wiki.phoenixlabs.org/wiki/P2P_Format |
---|
219 | * http://en.wikipedia.org/wiki/PeerGuardian#P2P_plaintext_format |
---|
220 | */ |
---|
221 | static bool |
---|
222 | parseLine1 (const char * line, struct tr_ipv4_range * range) |
---|
223 | { |
---|
224 | char * walk; |
---|
225 | int b[4]; |
---|
226 | int e[4]; |
---|
227 | char str[64]; |
---|
228 | tr_address addr; |
---|
229 | |
---|
230 | walk = strrchr (line, ':'); |
---|
231 | if (!walk) |
---|
232 | return false; |
---|
233 | ++walk; /* walk past the colon */ |
---|
234 | |
---|
235 | if (sscanf (walk, "%d.%d.%d.%d-%d.%d.%d.%d", |
---|
236 | &b[0], &b[1], &b[2], &b[3], |
---|
237 | &e[0], &e[1], &e[2], &e[3]) != 8) |
---|
238 | return false; |
---|
239 | |
---|
240 | tr_snprintf (str, sizeof (str), "%d.%d.%d.%d", b[0], b[1], b[2], b[3]); |
---|
241 | if (!tr_address_from_string (&addr, str)) |
---|
242 | return false; |
---|
243 | range->begin = ntohl (addr.addr.addr4.s_addr); |
---|
244 | |
---|
245 | tr_snprintf (str, sizeof (str), "%d.%d.%d.%d", e[0], e[1], e[2], e[3]); |
---|
246 | if (!tr_address_from_string (&addr, str)) |
---|
247 | return false; |
---|
248 | range->end = ntohl (addr.addr.addr4.s_addr); |
---|
249 | |
---|
250 | return true; |
---|
251 | } |
---|
252 | |
---|
253 | /* |
---|
254 | * DAT format: "000.000.000.000 - 000.255.255.255 , 000 , invalid ip" |
---|
255 | * http://wiki.phoenixlabs.org/wiki/DAT_Format |
---|
256 | */ |
---|
257 | static bool |
---|
258 | parseLine2 (const char * line, struct tr_ipv4_range * range) |
---|
259 | { |
---|
260 | int unk; |
---|
261 | int a[4]; |
---|
262 | int b[4]; |
---|
263 | char str[32]; |
---|
264 | tr_address addr; |
---|
265 | |
---|
266 | if (sscanf (line, "%3d.%3d.%3d.%3d - %3d.%3d.%3d.%3d , %3d , ", |
---|
267 | &a[0], &a[1], &a[2], &a[3], |
---|
268 | &b[0], &b[1], &b[2], &b[3], |
---|
269 | &unk) != 9) |
---|
270 | return false; |
---|
271 | |
---|
272 | tr_snprintf (str, sizeof (str), "%d.%d.%d.%d", a[0], a[1], a[2], a[3]); |
---|
273 | if (!tr_address_from_string (&addr, str)) |
---|
274 | return false; |
---|
275 | range->begin = ntohl (addr.addr.addr4.s_addr); |
---|
276 | |
---|
277 | tr_snprintf (str, sizeof (str), "%d.%d.%d.%d", b[0], b[1], b[2], b[3]); |
---|
278 | if (!tr_address_from_string (&addr, str)) |
---|
279 | return false; |
---|
280 | range->end = ntohl (addr.addr.addr4.s_addr); |
---|
281 | |
---|
282 | return true; |
---|
283 | } |
---|
284 | |
---|
285 | static bool |
---|
286 | parseLine (const char * line, struct tr_ipv4_range * range) |
---|
287 | { |
---|
288 | return parseLine1 (line, range) |
---|
289 | || parseLine2 (line, range); |
---|
290 | } |
---|
291 | |
---|
292 | static int |
---|
293 | compareAddressRangesByFirstAddress (const void * va, const void * vb) |
---|
294 | { |
---|
295 | const struct tr_ipv4_range * a = va; |
---|
296 | const struct tr_ipv4_range * b = vb; |
---|
297 | if (a->begin != b->begin) |
---|
298 | return a->begin < b->begin ? -1 : 1; |
---|
299 | return 0; |
---|
300 | } |
---|
301 | |
---|
302 | int |
---|
303 | tr_blocklistFileSetContent (tr_blocklistFile * b, const char * filename) |
---|
304 | { |
---|
305 | FILE * in; |
---|
306 | FILE * out; |
---|
307 | int inCount = 0; |
---|
308 | char line[2048]; |
---|
309 | const char * err_fmt = _("Couldn't read \"%1$s\": %2$s"); |
---|
310 | struct tr_ipv4_range * ranges = NULL; |
---|
311 | size_t ranges_alloc = 0; |
---|
312 | size_t ranges_count = 0; |
---|
313 | |
---|
314 | if (!filename) |
---|
315 | { |
---|
316 | blocklistDelete (b); |
---|
317 | return 0; |
---|
318 | } |
---|
319 | |
---|
320 | in = fopen (filename, "rb"); |
---|
321 | if (in == NULL) |
---|
322 | { |
---|
323 | tr_logAddError (err_fmt, filename, tr_strerror (errno)); |
---|
324 | return 0; |
---|
325 | } |
---|
326 | |
---|
327 | blocklistClose (b); |
---|
328 | |
---|
329 | out = fopen (b->filename, "wb+"); |
---|
330 | if (out == NULL) |
---|
331 | { |
---|
332 | tr_logAddError (err_fmt, b->filename, tr_strerror (errno)); |
---|
333 | fclose (in); |
---|
334 | return 0; |
---|
335 | } |
---|
336 | |
---|
337 | /* load the rules into memory */ |
---|
338 | while (fgets (line, sizeof (line), in) != NULL) |
---|
339 | { |
---|
340 | char * walk; |
---|
341 | struct tr_ipv4_range range; |
---|
342 | |
---|
343 | ++inCount; |
---|
344 | |
---|
345 | /* zap the linefeed */ |
---|
346 | if ((walk = strchr (line, '\r'))) *walk = '\0'; |
---|
347 | if ((walk = strchr (line, '\n'))) *walk = '\0'; |
---|
348 | |
---|
349 | if (!parseLine (line, &range)) |
---|
350 | { |
---|
351 | /* don't try to display the actual lines - it causes issues */ |
---|
352 | tr_logAddError (_("blocklist skipped invalid address at line %d"), inCount); |
---|
353 | continue; |
---|
354 | } |
---|
355 | |
---|
356 | if (ranges_alloc == ranges_count) |
---|
357 | { |
---|
358 | ranges_alloc += 4096; /* arbitrary */ |
---|
359 | ranges = tr_renew (struct tr_ipv4_range, ranges, ranges_alloc); |
---|
360 | } |
---|
361 | |
---|
362 | ranges[ranges_count++] = range; |
---|
363 | } |
---|
364 | |
---|
365 | if (ranges_count > 0) /* sort and merge */ |
---|
366 | { |
---|
367 | struct tr_ipv4_range * r; |
---|
368 | struct tr_ipv4_range * keep = ranges; |
---|
369 | const struct tr_ipv4_range * end; |
---|
370 | |
---|
371 | /* sort */ |
---|
372 | qsort (ranges, ranges_count, sizeof (struct tr_ipv4_range), |
---|
373 | compareAddressRangesByFirstAddress); |
---|
374 | |
---|
375 | /* merge */ |
---|
376 | for (r=ranges+1, end=ranges+ranges_count; r!=end; ++r) { |
---|
377 | if (keep->end < r->begin) |
---|
378 | *++keep = *r; |
---|
379 | else if (keep->end < r->end) |
---|
380 | keep->end = r->end; |
---|
381 | } |
---|
382 | |
---|
383 | ranges_count = keep + 1 - ranges; |
---|
384 | |
---|
385 | #ifndef NDEBUG |
---|
386 | /* sanity checks: make sure the rules are sorted |
---|
387 | * in ascending order and don't overlap */ |
---|
388 | { |
---|
389 | size_t i; |
---|
390 | |
---|
391 | for (i=0; i<ranges_count; ++i) |
---|
392 | assert (ranges[i].begin <= ranges[i].end); |
---|
393 | |
---|
394 | for (i=1; i<ranges_count; ++i) |
---|
395 | assert (ranges[i-1].end < ranges[i].begin); |
---|
396 | } |
---|
397 | #endif |
---|
398 | } |
---|
399 | |
---|
400 | if (fwrite (ranges, sizeof (struct tr_ipv4_range), ranges_count, out) != ranges_count) |
---|
401 | { |
---|
402 | tr_logAddError (_("Couldn't save file \"%1$s\": %2$s"), b->filename, tr_strerror (errno)); |
---|
403 | } |
---|
404 | else |
---|
405 | { |
---|
406 | char * base = tr_sys_path_basename (b->filename, NULL); |
---|
407 | tr_logAddInfo (_("Blocklist \"%s\" updated with %"TR_PRIuSIZE" entries"), base, ranges_count); |
---|
408 | tr_free (base); |
---|
409 | } |
---|
410 | |
---|
411 | tr_free (ranges); |
---|
412 | fclose (out); |
---|
413 | fclose (in); |
---|
414 | |
---|
415 | blocklistLoad (b); |
---|
416 | |
---|
417 | return ranges_count; |
---|
418 | } |
---|