source: trunk/libtransmission/blocklist.c

Last change on this file was 14644, checked in by mikedld, 5 years ago

Remove useless checks and definitions (C99)

Now that MSVC support for C99 is quite good, remove previously needed but
now unused checks and definitions, like PRI* format macros (including
PRIdMAX and TR_PRIuSIZE, replaced with %jd and %zu) and inline macro.
Also, remove ssize_t typedef and replace few occurences with ev_ssize_t.
Also, remove check for stdbool.h availability (guaranteed by C99) and
include it unconditionally (except when in C++ mode).

  • Property svn:keywords set to Date Rev Author Id
File size: 9.2 KB
Line 
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 14644 2015-12-29 19:37:31Z mikedld $
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
29struct tr_ipv4_range
30{
31  uint32_t begin;
32  uint32_t end;
33};
34
35struct 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
45static void
46blocklistClose (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
59static void
60blocklistLoad (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 %zu entries"), base, b->ruleCount);
101  tr_free (base);
102}
103
104static void
105blocklistEnsureLoaded (tr_blocklistFile * b)
106{
107  if (b->rules == NULL)
108    blocklistLoad (b);
109}
110
111static int
112compareAddressToRange (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
122static void
123blocklistDelete (tr_blocklistFile * b)
124{
125  blocklistClose (b);
126  tr_sys_path_remove (b->filename, NULL);
127}
128
129/***
130****  PACKAGE-VISIBLE
131***/
132
133tr_blocklistFile *
134tr_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
146const char*
147tr_blocklistFileGetFilename (const tr_blocklistFile * b)
148{
149  return b->filename;
150}
151
152void
153tr_blocklistFileFree (tr_blocklistFile * b)
154{
155  blocklistClose (b);
156  tr_free (b->filename);
157  tr_free (b);
158}
159
160bool
161tr_blocklistFileExists (const tr_blocklistFile * b)
162{
163  return tr_sys_path_exists (b->filename, NULL);
164}
165
166int
167tr_blocklistFileGetRuleCount (const tr_blocklistFile * b)
168{
169  blocklistEnsureLoaded ((tr_blocklistFile*)b);
170
171  return b->ruleCount;
172}
173
174bool
175tr_blocklistFileIsEnabled (tr_blocklistFile * b)
176{
177  return b->isEnabled;
178}
179
180void
181tr_blocklistFileSetEnabled (tr_blocklistFile * b, bool isEnabled)
182{
183  assert (b != NULL);
184  assert (tr_isBool (isEnabled));
185
186  b->isEnabled = isEnabled;
187}
188
189bool
190tr_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 */
221static bool
222parseLine1 (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 */
257static bool
258parseLine2 (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
285static bool
286parseLine (const char * line, struct tr_ipv4_range * range)
287{
288  return parseLine1 (line, range)
289      || parseLine2 (line, range);
290}
291
292static int
293compareAddressRangesByFirstAddress (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
302int
303tr_blocklistFileSetContent (tr_blocklistFile * b, const char * filename)
304{
305  tr_sys_file_t in;
306  tr_sys_file_t 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  tr_error * error = NULL;
314
315  if (!filename)
316    {
317      blocklistDelete (b);
318      return 0;
319    }
320
321  in = tr_sys_file_open (filename, TR_SYS_FILE_READ, 0, &error);
322  if (in == TR_BAD_SYS_FILE)
323    {
324      tr_logAddError (err_fmt, filename, error->message);
325      tr_error_free (error);
326      return 0;
327    }
328
329  blocklistClose (b);
330
331  out = tr_sys_file_open (b->filename,
332                          TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE,
333                          0666, &error);
334  if (out == TR_BAD_SYS_FILE)
335    {
336      tr_logAddError (err_fmt, b->filename, error->message);
337      tr_error_free (error);
338      tr_sys_file_close (in, NULL);
339      return 0;
340    }
341
342  /* load the rules into memory */
343  while (tr_sys_file_read_line (in, line, sizeof (line), NULL))
344    {
345      struct tr_ipv4_range range;
346
347      ++inCount;
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 (!tr_sys_file_write (out, ranges, sizeof (struct tr_ipv4_range) * ranges_count, NULL, &error))
401    {
402      tr_logAddError (_("Couldn't save file \"%1$s\": %2$s"), b->filename, error->message);
403      tr_error_free (error);
404    }
405  else
406    {
407      char * base = tr_sys_path_basename (b->filename, NULL);
408      tr_logAddInfo (_("Blocklist \"%s\" updated with %zu entries"), base, ranges_count);
409      tr_free (base);
410    }
411
412  tr_free (ranges);
413  tr_sys_file_close (out, NULL);
414  tr_sys_file_close (in, NULL);
415
416  blocklistLoad (b);
417
418  return ranges_count;
419}
Note: See TracBrowser for help on using the repository browser.