2008-03-29 18:37:07 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2008-03-29 18:37:07 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2012-12-05 17:29:46 +00:00
|
|
|
* Transmission project are granted a special exemption to clause 2 (b)
|
2008-09-23 19:11:04 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2008-03-29 18:37:07 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2008-05-28 17:17:12 +00:00
|
|
|
* $Id$
|
2008-03-29 18:37:07 +00:00
|
|
|
*/
|
|
|
|
|
2011-03-24 21:49:42 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2008-03-29 18:37:07 +00:00
|
|
|
#include <stdio.h>
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <stdlib.h> /* bsearch (), qsort () */
|
2008-03-29 18:37:07 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <unistd.h> /* unlink () */
|
2011-03-24 21:49:42 +00:00
|
|
|
|
2008-09-05 14:31:52 +00:00
|
|
|
#ifdef WIN32
|
2010-06-22 00:12:52 +00:00
|
|
|
#include <w32api.h>
|
|
|
|
#define WINVER WindowsXP
|
2008-09-23 19:11:04 +00:00
|
|
|
#include <windows.h>
|
2010-06-25 20:36:10 +00:00
|
|
|
#define PROT_READ PAGE_READONLY
|
|
|
|
#define MAP_PRIVATE FILE_MAP_COPY
|
2008-09-05 14:31:52 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WIN32
|
2008-09-23 19:11:04 +00:00
|
|
|
#include <sys/mman.h>
|
2008-09-05 14:31:52 +00:00
|
|
|
#endif
|
2008-03-29 18:37:07 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "transmission.h"
|
2008-03-30 13:22:45 +00:00
|
|
|
#include "blocklist.h"
|
2008-12-02 03:41:58 +00:00
|
|
|
#include "net.h"
|
2008-04-01 19:20:21 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2010-06-22 00:12:52 +00:00
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2008-12-09 21:39:05 +00:00
|
|
|
|
2008-04-01 19:20:21 +00:00
|
|
|
/***
|
|
|
|
**** PRIVATE
|
|
|
|
***/
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2011-02-12 21:20:19 +00:00
|
|
|
struct tr_ipv4_range
|
2008-03-29 21:05:51 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
uint32_t begin;
|
|
|
|
uint32_t end;
|
2008-03-29 21:05:51 +00:00
|
|
|
};
|
|
|
|
|
2008-03-30 13:22:45 +00:00
|
|
|
struct tr_blocklist
|
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
bool isEnabled;
|
2011-02-12 21:20:19 +00:00
|
|
|
int fd;
|
|
|
|
size_t ruleCount;
|
|
|
|
size_t byteCount;
|
|
|
|
char * filename;
|
|
|
|
struct tr_ipv4_range * rules;
|
2008-03-30 13:22:45 +00:00
|
|
|
};
|
2008-03-29 18:37:07 +00:00
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
blocklistClose (tr_blocklist * b)
|
2008-03-29 18:37:07 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (b->rules)
|
2008-03-29 18:37:07 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
munmap (b->rules, b->byteCount);
|
|
|
|
close (b->fd);
|
2008-03-30 13:22:45 +00:00
|
|
|
b->rules = NULL;
|
|
|
|
b->ruleCount = 0;
|
|
|
|
b->byteCount = 0;
|
|
|
|
b->fd = -1;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
blocklistLoad (tr_blocklist * b)
|
2008-03-29 18:37:07 +00:00
|
|
|
{
|
2010-07-01 15:14:35 +00:00
|
|
|
int fd;
|
|
|
|
size_t byteCount;
|
|
|
|
struct stat st;
|
2012-12-07 01:53:31 +00:00
|
|
|
const char * err_fmt = _("Couldn't read \"%1$s\": %2$s");
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
blocklistClose (b);
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (stat (b->filename, &st) == -1)
|
2008-04-05 16:45:35 +00:00
|
|
|
return;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
fd = open (b->filename, O_RDONLY | O_BINARY);
|
|
|
|
if (fd == -1)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_err (err_fmt, b->filename, tr_strerror (errno));
|
2008-03-29 18:37:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-04-05 16:45:35 +00:00
|
|
|
|
2010-07-01 15:14:35 +00:00
|
|
|
byteCount = (size_t) st.st_size;
|
2012-12-05 17:29:46 +00:00
|
|
|
b->rules = mmap (NULL, byteCount, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
|
|
if (!b->rules)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_err (err_fmt, b->filename, tr_strerror (errno));
|
|
|
|
close (fd);
|
2008-03-29 18:37:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-30 13:22:45 +00:00
|
|
|
b->fd = fd;
|
2010-07-01 15:14:35 +00:00
|
|
|
b->byteCount = byteCount;
|
2012-12-05 17:29:46 +00:00
|
|
|
b->ruleCount = byteCount / sizeof (struct tr_ipv4_range);
|
2008-06-11 16:15:45 +00:00
|
|
|
|
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char * base = tr_basename (b->filename);
|
2012-12-07 01:53:31 +00:00
|
|
|
tr_inf (_("Blocklist \"%s\" contains %zu entries"), base, b->ruleCount);
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (base);
|
2008-06-11 16:15:45 +00:00
|
|
|
}
|
2008-03-29 21:05:51 +00:00
|
|
|
}
|
|
|
|
|
2008-03-30 00:00:28 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
blocklistEnsureLoaded (tr_blocklist * b)
|
2008-03-30 00:00:28 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!b->rules)
|
|
|
|
blocklistLoad (b);
|
2008-03-30 00:00:28 +00:00
|
|
|
}
|
|
|
|
|
2008-03-29 21:05:51 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
compareAddressToRange (const void * va,
|
|
|
|
const void * vb)
|
2008-03-29 21:05:51 +00:00
|
|
|
{
|
2011-02-12 21:20:19 +00:00
|
|
|
const uint32_t * a = va;
|
|
|
|
const struct tr_ipv4_range * b = vb;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (*a < b->begin) return -1;
|
|
|
|
if (*a > b->end) return 1;
|
2008-03-29 21:05:51 +00:00
|
|
|
return 0;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
|
|
|
|
2008-03-30 13:22:45 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
blocklistDelete (tr_blocklist * b)
|
2008-03-30 13:22:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
blocklistClose (b);
|
|
|
|
unlink (b->filename);
|
2008-03-30 13:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
2008-04-01 19:20:21 +00:00
|
|
|
**** PACKAGE-VISIBLE
|
2008-03-30 13:22:45 +00:00
|
|
|
***/
|
|
|
|
|
|
|
|
tr_blocklist *
|
2012-12-05 17:29:46 +00:00
|
|
|
_tr_blocklistNew (const char * filename, bool isEnabled)
|
2008-03-30 13:22:45 +00:00
|
|
|
{
|
|
|
|
tr_blocklist * b;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
b = tr_new0 (tr_blocklist, 1);
|
2008-03-30 13:22:45 +00:00
|
|
|
b->fd = -1;
|
2012-12-05 17:29:46 +00:00
|
|
|
b->filename = tr_strdup (filename);
|
2008-03-30 13:22:45 +00:00
|
|
|
b->isEnabled = isEnabled;
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2008-06-11 16:15:45 +00:00
|
|
|
const char*
|
2012-12-05 17:29:46 +00:00
|
|
|
_tr_blocklistGetFilename (const tr_blocklist * b)
|
2008-06-11 16:15:45 +00:00
|
|
|
{
|
|
|
|
return b->filename;
|
|
|
|
}
|
|
|
|
|
2008-03-30 13:22:45 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
_tr_blocklistFree (tr_blocklist * b)
|
2008-03-30 13:22:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
blocklistClose (b);
|
|
|
|
tr_free (b->filename);
|
|
|
|
tr_free (b);
|
2008-03-30 13:22:45 +00:00
|
|
|
}
|
|
|
|
|
2008-03-29 23:12:34 +00:00
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
_tr_blocklistExists (const tr_blocklist * b)
|
2008-03-29 23:12:34 +00:00
|
|
|
{
|
2008-06-11 16:15:45 +00:00
|
|
|
struct stat st;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return !stat (b->filename, &st);
|
2008-06-11 16:15:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
_tr_blocklistGetRuleCount (const tr_blocklist * b)
|
2008-06-11 16:15:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
blocklistEnsureLoaded ((tr_blocklist*)b);
|
2008-03-30 13:22:45 +00:00
|
|
|
|
|
|
|
return b->ruleCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
_tr_blocklistIsEnabled (tr_blocklist * b)
|
2008-03-30 13:22:45 +00:00
|
|
|
{
|
2008-04-01 19:20:21 +00:00
|
|
|
return b->isEnabled;
|
2008-03-29 23:12:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
_tr_blocklistSetEnabled (tr_blocklist * b, bool isEnabled)
|
2008-03-29 23:12:34 +00:00
|
|
|
{
|
2008-04-01 19:20:21 +00:00
|
|
|
b->isEnabled = isEnabled ? 1 : 0;
|
2008-03-29 23:12:34 +00:00
|
|
|
}
|
|
|
|
|
2008-03-29 18:37:07 +00:00
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
_tr_blocklistHasAddress (tr_blocklist * b, const tr_address * addr)
|
2008-03-29 18:37:07 +00:00
|
|
|
{
|
2011-02-12 21:20:19 +00:00
|
|
|
uint32_t needle;
|
|
|
|
const struct tr_ipv4_range * range;
|
2008-03-29 21:05:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
assert (tr_address_is_valid (addr));
|
2008-12-20 02:56:44 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!b->isEnabled || addr->type == TR_AF_INET6)
|
2008-03-30 13:22:45 +00:00
|
|
|
return 0;
|
2008-03-29 23:12:34 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
blocklistEnsureLoaded (b);
|
2009-12-14 18:47:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!b->rules || !b->ruleCount)
|
2008-03-30 13:22:45 +00:00
|
|
|
return 0;
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
needle = ntohl (addr->addr.addr4.s_addr);
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
range = bsearch (&needle,
|
2008-03-30 13:22:45 +00:00
|
|
|
b->rules,
|
|
|
|
b->ruleCount,
|
2012-12-05 17:29:46 +00:00
|
|
|
sizeof (struct tr_ipv4_range),
|
|
|
|
compareAddressToRange);
|
2008-03-29 21:05:51 +00:00
|
|
|
|
|
|
|
return range != NULL;
|
|
|
|
}
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2010-05-31 11:38:36 +00:00
|
|
|
/*
|
2010-11-07 02:53:50 +00:00
|
|
|
* P2P plaintext format: "comment:x.x.x.x-y.y.y.y"
|
|
|
|
* http://wiki.phoenixlabs.org/wiki/P2P_Format
|
|
|
|
* http://en.wikipedia.org/wiki/PeerGuardian#P2P_plaintext_format
|
2010-05-31 11:38:36 +00:00
|
|
|
*/
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool
|
2012-12-05 17:29:46 +00:00
|
|
|
parseLine1 (const char * line, struct tr_ipv4_range * range)
|
2010-05-31 11:38:36 +00:00
|
|
|
{
|
|
|
|
char * walk;
|
|
|
|
int b[4];
|
|
|
|
int e[4];
|
|
|
|
char str[64];
|
|
|
|
tr_address addr;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
walk = strrchr (line, ':');
|
|
|
|
if (!walk)
|
2011-03-22 15:19:54 +00:00
|
|
|
return false;
|
2010-05-31 11:38:36 +00:00
|
|
|
++walk; /* walk past the colon */
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (sscanf (walk, "%d.%d.%d.%d-%d.%d.%d.%d",
|
2010-05-31 11:38:36 +00:00
|
|
|
&b[0], &b[1], &b[2], &b[3],
|
2012-12-05 17:29:46 +00:00
|
|
|
&e[0], &e[1], &e[2], &e[3]) != 8)
|
2011-03-22 15:19:54 +00:00
|
|
|
return false;
|
2010-05-31 11:38:36 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_snprintf (str, sizeof (str), "%d.%d.%d.%d", b[0], b[1], b[2], b[3]);
|
|
|
|
if (!tr_address_from_string (&addr, str))
|
2011-03-22 15:19:54 +00:00
|
|
|
return false;
|
2012-12-05 17:29:46 +00:00
|
|
|
range->begin = ntohl (addr.addr.addr4.s_addr);
|
2010-05-31 11:38:36 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_snprintf (str, sizeof (str), "%d.%d.%d.%d", e[0], e[1], e[2], e[3]);
|
|
|
|
if (!tr_address_from_string (&addr, str))
|
2011-03-22 15:19:54 +00:00
|
|
|
return false;
|
2012-12-05 17:29:46 +00:00
|
|
|
range->end = ntohl (addr.addr.addr4.s_addr);
|
2010-05-31 11:38:36 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
return true;
|
2010-05-31 11:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-11-07 02:53:50 +00:00
|
|
|
* DAT format: "000.000.000.000 - 000.255.255.255 , 000 , invalid ip"
|
|
|
|
* http://wiki.phoenixlabs.org/wiki/DAT_Format
|
2010-05-31 11:38:36 +00:00
|
|
|
*/
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool
|
2012-12-05 17:29:46 +00:00
|
|
|
parseLine2 (const char * line, struct tr_ipv4_range * range)
|
2010-05-31 11:38:36 +00:00
|
|
|
{
|
|
|
|
int unk;
|
|
|
|
int a[4];
|
|
|
|
int b[4];
|
|
|
|
char str[32];
|
|
|
|
tr_address addr;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (sscanf (line, "%3d.%3d.%3d.%3d - %3d.%3d.%3d.%3d , %3d , ",
|
2010-05-31 11:38:36 +00:00
|
|
|
&a[0], &a[1], &a[2], &a[3],
|
|
|
|
&b[0], &b[1], &b[2], &b[3],
|
2012-12-05 17:29:46 +00:00
|
|
|
&unk) != 9)
|
2011-03-22 15:19:54 +00:00
|
|
|
return false;
|
2010-05-31 11:38:36 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_snprintf (str, sizeof (str), "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
|
|
|
|
if (!tr_address_from_string (&addr, str))
|
2011-03-22 15:19:54 +00:00
|
|
|
return false;
|
2012-12-05 17:29:46 +00:00
|
|
|
range->begin = ntohl (addr.addr.addr4.s_addr);
|
2010-05-31 11:38:36 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_snprintf (str, sizeof (str), "%d.%d.%d.%d", b[0], b[1], b[2], b[3]);
|
|
|
|
if (!tr_address_from_string (&addr, str))
|
2011-03-22 15:19:54 +00:00
|
|
|
return false;
|
2012-12-05 17:29:46 +00:00
|
|
|
range->end = ntohl (addr.addr.addr4.s_addr);
|
2010-05-31 11:38:36 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
return true;
|
2010-05-31 11:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
parseLine (const char * line, struct tr_ipv4_range * range)
|
2010-05-31 11:38:36 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return parseLine1 (line, range)
|
|
|
|
|| parseLine2 (line, range);
|
2010-05-31 11:38:36 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 16:14:31 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
compareAddressRangesByFirstAddress (const void * va, const void * vb)
|
2011-02-13 16:14:31 +00:00
|
|
|
{
|
|
|
|
const struct tr_ipv4_range * a = va;
|
|
|
|
const struct tr_ipv4_range * b = vb;
|
2012-12-05 17:29:46 +00:00
|
|
|
if (a->begin != b->begin)
|
2011-02-13 16:14:31 +00:00
|
|
|
return a->begin < b->begin ? -1 : 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-30 00:00:28 +00:00
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
_tr_blocklistSetContent (tr_blocklist * b, const char * filename)
|
2008-03-29 18:37:07 +00:00
|
|
|
{
|
2010-06-15 00:38:10 +00:00
|
|
|
FILE * in;
|
|
|
|
FILE * out;
|
|
|
|
int inCount = 0;
|
|
|
|
char line[2048];
|
2012-12-07 01:53:31 +00:00
|
|
|
const char * err_fmt = _("Couldn't read \"%1$s\": %2$s");
|
2011-02-13 16:14:31 +00:00
|
|
|
struct tr_ipv4_range * ranges = NULL;
|
|
|
|
size_t ranges_alloc = 0;
|
|
|
|
size_t ranges_count = 0;
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!filename)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
blocklistDelete (b);
|
2008-03-30 00:00:28 +00:00
|
|
|
return 0;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
in = fopen (filename, "rb");
|
|
|
|
if (!in)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_err (err_fmt, filename, tr_strerror (errno));
|
2008-03-30 00:00:28 +00:00
|
|
|
return 0;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
blocklistClose (b);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
out = fopen (b->filename, "wb+");
|
|
|
|
if (!out)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_err (err_fmt, b->filename, tr_strerror (errno));
|
|
|
|
fclose (in);
|
2008-03-30 00:00:28 +00:00
|
|
|
return 0;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 16:14:31 +00:00
|
|
|
/* load the rules into memory */
|
2012-12-05 17:29:46 +00:00
|
|
|
while (fgets (line, sizeof (line), in) != NULL)
|
2008-03-29 18:37:07 +00:00
|
|
|
{
|
2010-05-31 11:38:36 +00:00
|
|
|
char * walk;
|
2011-02-12 21:20:19 +00:00
|
|
|
struct tr_ipv4_range range;
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2010-04-27 03:58:25 +00:00
|
|
|
++inCount;
|
|
|
|
|
2010-05-31 11:38:36 +00:00
|
|
|
/* zap the linefeed */
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((walk = strchr (line, '\r'))) *walk = '\0';
|
|
|
|
if ((walk = strchr (line, '\n'))) *walk = '\0';
|
2008-03-29 21:05:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!parseLine (line, &range))
|
2010-04-30 22:58:03 +00:00
|
|
|
{
|
2010-05-02 18:03:49 +00:00
|
|
|
/* don't try to display the actual lines - it causes issues */
|
2012-12-07 01:53:31 +00:00
|
|
|
tr_err (_("blocklist skipped invalid address at line %d"), inCount);
|
2010-04-30 22:58:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (ranges_alloc == ranges_count)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2011-02-13 16:14:31 +00:00
|
|
|
ranges_alloc += 4096; /* arbitrary */
|
2012-12-05 17:29:46 +00:00
|
|
|
ranges = tr_renew (struct tr_ipv4_range, ranges, ranges_alloc);
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
2008-03-29 21:05:51 +00:00
|
|
|
|
2011-02-13 16:14:31 +00:00
|
|
|
ranges[ranges_count++] = range;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (ranges_count > 0) /* sort and merge */
|
2008-06-11 16:15:45 +00:00
|
|
|
{
|
2011-02-13 16:14:31 +00:00
|
|
|
struct tr_ipv4_range * r;
|
|
|
|
struct tr_ipv4_range * keep = ranges;
|
|
|
|
const struct tr_ipv4_range * end;
|
|
|
|
|
|
|
|
/* sort */
|
2012-12-05 17:29:46 +00:00
|
|
|
qsort (ranges, ranges_count, sizeof (struct tr_ipv4_range),
|
|
|
|
compareAddressRangesByFirstAddress);
|
2011-02-13 16:14:31 +00:00
|
|
|
|
|
|
|
/* merge */
|
2012-12-05 17:29:46 +00:00
|
|
|
for (r=ranges+1, end=ranges+ranges_count; r!=end; ++r) {
|
|
|
|
if (keep->end < r->begin)
|
2011-02-13 16:14:31 +00:00
|
|
|
*++keep = *r;
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (keep->end < r->end)
|
2011-02-13 16:14:31 +00:00
|
|
|
keep->end = r->end;
|
|
|
|
}
|
|
|
|
|
|
|
|
ranges_count = keep + 1 - ranges;
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
/* sanity checks: make sure the rules are sorted
|
|
|
|
* in ascending order and don't overlap */
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; i<ranges_count; ++i)
|
|
|
|
assert (ranges[i].begin <= ranges[i].end);
|
2011-02-13 16:14:31 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=1; i<ranges_count; ++i)
|
|
|
|
assert (ranges[i-1].end < ranges[i].begin);
|
2011-02-13 16:14:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (fwrite (ranges, sizeof (struct tr_ipv4_range), ranges_count, out) != ranges_count)
|
2012-12-07 01:53:31 +00:00
|
|
|
tr_err (_("Couldn't save file \"%1$s\": %2$s"), b->filename, tr_strerror (errno));
|
2011-02-13 16:14:31 +00:00
|
|
|
else {
|
2012-12-05 17:29:46 +00:00
|
|
|
char * base = tr_basename (b->filename);
|
2012-12-07 01:53:31 +00:00
|
|
|
tr_inf (_("Blocklist \"%s\" updated with %zu entries"), base, ranges_count);
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (base);
|
2008-06-11 16:15:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (ranges);
|
|
|
|
fclose (out);
|
|
|
|
fclose (in);
|
2008-03-29 21:05:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
blocklistLoad (b);
|
2008-03-30 00:00:28 +00:00
|
|
|
|
2011-02-13 16:14:31 +00:00
|
|
|
return ranges_count;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|