2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2008-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib> /* bsearch(), qsort() */
|
|
|
|
#include <cstring>
|
2008-03-29 18:37:07 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
2008-03-30 13:22:45 +00:00
|
|
|
#include "blocklist.h"
|
2014-07-28 04:13:38 +00:00
|
|
|
#include "error.h"
|
2014-07-08 00:08:43 +00:00
|
|
|
#include "file.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2008-12-02 03:41:58 +00:00
|
|
|
#include "net.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2008-04-01 19:20:21 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
/***
|
|
|
|
**** 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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
uint32_t begin;
|
|
|
|
uint32_t end;
|
2008-03-29 21:05:51 +00:00
|
|
|
};
|
|
|
|
|
2013-01-21 17:48:36 +00:00
|
|
|
struct tr_blocklistFile
|
2008-03-30 13:22:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool isEnabled;
|
|
|
|
tr_sys_file_t fd;
|
|
|
|
size_t ruleCount;
|
|
|
|
uint64_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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void blocklistClose(tr_blocklistFile* b)
|
2008-03-29 18:37:07 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (b->rules != nullptr)
|
2008-03-29 18:37:07 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_sys_file_unmap(b->rules, b->byteCount, nullptr);
|
|
|
|
tr_sys_file_close(b->fd, nullptr);
|
|
|
|
b->rules = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
b->ruleCount = 0;
|
|
|
|
b->byteCount = 0;
|
|
|
|
b->fd = TR_BAD_SYS_FILE;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void blocklistLoad(tr_blocklistFile* b)
|
2008-03-29 18:37:07 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_error* error = nullptr;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* err_fmt = _("Couldn't read \"%1$s\": %2$s");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
blocklistClose(b);
|
|
|
|
|
2021-11-03 03:55:43 +00:00
|
|
|
auto info = tr_sys_path_info{};
|
2021-09-15 00:18:09 +00:00
|
|
|
if (!tr_sys_path_get_info(b->filename, 0, &info, nullptr))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2021-11-03 03:55:43 +00:00
|
|
|
auto const byteCount = info.size;
|
2017-04-19 12:04:45 +00:00
|
|
|
if (byteCount == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-07-08 00:08:43 +00:00
|
|
|
|
2021-11-03 03:55:43 +00:00
|
|
|
auto const fd = tr_sys_file_open(b->filename, TR_SYS_FILE_READ, 0, &error);
|
2017-04-19 12:04:45 +00:00
|
|
|
if (fd == TR_BAD_SYS_FILE)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddError(err_fmt, b->filename, error->message);
|
|
|
|
tr_error_free(error);
|
|
|
|
return;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
2008-04-05 16:45:35 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
b->rules = static_cast<struct tr_ipv4_range*>(tr_sys_file_map_for_reading(fd, 0, byteCount, &error));
|
2021-09-15 00:18:09 +00:00
|
|
|
if (b->rules == nullptr)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddError(err_fmt, b->filename, error->message);
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_sys_file_close(fd, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error_free(error);
|
|
|
|
return;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
b->fd = fd;
|
|
|
|
b->byteCount = byteCount;
|
|
|
|
b->ruleCount = byteCount / sizeof(struct tr_ipv4_range);
|
2008-06-11 16:15:45 +00:00
|
|
|
|
2021-11-03 03:55:43 +00:00
|
|
|
char* const base = tr_sys_path_basename(b->filename, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddInfo(_("Blocklist \"%s\" contains %zu entries"), base, b->ruleCount);
|
|
|
|
tr_free(base);
|
2008-03-29 21:05:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void blocklistEnsureLoaded(tr_blocklistFile* b)
|
2008-03-30 00:00:28 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (b->rules == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
blocklistLoad(b);
|
|
|
|
}
|
2008-03-30 00:00:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int compareAddressToRange(void const* va, void const* vb)
|
2008-03-29 21:05:51 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto const* a = static_cast<uint32_t const*>(va);
|
|
|
|
auto const* b = static_cast<struct tr_ipv4_range const*>(vb);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (*a < b->begin)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*a > b->end)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void blocklistDelete(tr_blocklistFile* b)
|
2008-03-30 13:22:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
blocklistClose(b);
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_sys_path_remove(b->filename, nullptr);
|
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
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_blocklistFile* tr_blocklistFileNew(char const* filename, bool isEnabled)
|
2008-03-30 13:22:45 +00:00
|
|
|
{
|
2021-11-03 03:55:43 +00:00
|
|
|
auto* const b = tr_new0(tr_blocklistFile, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
b->fd = TR_BAD_SYS_FILE;
|
|
|
|
b->filename = tr_strdup(filename);
|
|
|
|
b->isEnabled = isEnabled;
|
2008-03-30 13:22:45 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return b;
|
2008-03-30 13:22:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_blocklistFileGetFilename(tr_blocklistFile const* b)
|
2008-06-11 16:15:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return b->filename;
|
2008-06-11 16:15:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_blocklistFileFree(tr_blocklistFile* b)
|
2008-03-30 13:22:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
blocklistClose(b);
|
|
|
|
tr_free(b->filename);
|
|
|
|
tr_free(b);
|
2008-03-30 13:22:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_blocklistFileExists(tr_blocklistFile const* b)
|
2008-03-29 23:12:34 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return tr_sys_path_exists(b->filename, nullptr);
|
2008-06-11 16:15:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int tr_blocklistFileGetRuleCount(tr_blocklistFile const* b)
|
2008-06-11 16:15:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
blocklistEnsureLoaded((tr_blocklistFile*)b);
|
2008-03-30 13:22:45 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return b->ruleCount;
|
2008-03-30 13:22:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-01 17:30:51 +00:00
|
|
|
bool tr_blocklistFileIsEnabled(tr_blocklistFile* b)
|
2008-03-30 13:22:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return b->isEnabled;
|
2008-03-29 23:12:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_blocklistFileSetEnabled(tr_blocklistFile* b, bool isEnabled)
|
2008-03-29 23:12:34 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(b != nullptr);
|
2013-08-24 18:18:38 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
b->isEnabled = isEnabled;
|
2008-03-29 23:12:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_blocklistFileHasAddress(tr_blocklistFile* b, tr_address const* addr)
|
2008-03-29 18:37:07 +00:00
|
|
|
{
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(tr_address_is_valid(addr));
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!b->isEnabled || addr->type == TR_AF_INET6)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2008-03-29 23:12:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
blocklistEnsureLoaded(b);
|
2009-12-14 18:47:45 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (b->rules == nullptr || b->ruleCount == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2021-11-03 03:55:43 +00:00
|
|
|
auto const needle = ntohl(addr->addr.addr4.s_addr);
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
auto const* range = static_cast<struct tr_ipv4_range const*>(
|
|
|
|
bsearch(&needle, b->rules, b->ruleCount, sizeof(struct tr_ipv4_range), compareAddressToRange));
|
2008-03-29 21:05:51 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
return range != nullptr;
|
2008-03-29 21:05:51 +00:00
|
|
|
}
|
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
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool parseLine1(char const* line, struct tr_ipv4_range* range)
|
2010-05-31 11:38:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int b[4];
|
|
|
|
int e[4];
|
|
|
|
char str[64];
|
|
|
|
tr_address addr;
|
|
|
|
|
2020-11-09 03:31:02 +00:00
|
|
|
char const* walk = strrchr(line, ':');
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (walk == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
++walk; /* walk past the colon */
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if (sscanf(
|
|
|
|
walk,
|
|
|
|
"%d.%d.%d.%d-%d.%d.%d.%d",
|
|
|
|
TR_ARG_TUPLE(&b[0], &b[1], &b[2], &b[3]),
|
|
|
|
TR_ARG_TUPLE(&e[0], &e[1], &e[2], &e[3])) != 8)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_snprintf(str, sizeof(str), "%d.%d.%d.%d", TR_ARG_TUPLE(b[0], b[1], b[2], b[3]));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (!tr_address_from_string(&addr, str))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
range->begin = ntohl(addr.addr.addr4.s_addr);
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_snprintf(str, sizeof(str), "%d.%d.%d.%d", TR_ARG_TUPLE(e[0], e[1], e[2], e[3]));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (!tr_address_from_string(&addr, str))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
range->end = ntohl(addr.addr.addr4.s_addr);
|
|
|
|
|
|
|
|
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
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool parseLine2(char const* line, struct tr_ipv4_range* range)
|
2010-05-31 11:38:36 +00:00
|
|
|
{
|
2021-11-03 03:55:43 +00:00
|
|
|
int unk = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
int a[4];
|
|
|
|
int b[4];
|
|
|
|
char str[32];
|
|
|
|
tr_address addr;
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if (sscanf(
|
|
|
|
line,
|
|
|
|
"%3d.%3d.%3d.%3d - %3d.%3d.%3d.%3d , %3d , ",
|
|
|
|
TR_ARG_TUPLE(&a[0], &a[1], &a[2], &a[3]),
|
|
|
|
TR_ARG_TUPLE(&b[0], &b[1], &b[2], &b[3]),
|
|
|
|
&unk) != 9)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_snprintf(str, sizeof(str), "%d.%d.%d.%d", TR_ARG_TUPLE(a[0], a[1], a[2], a[3]));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (!tr_address_from_string(&addr, str))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
range->begin = ntohl(addr.addr.addr4.s_addr);
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_snprintf(str, sizeof(str), "%d.%d.%d.%d", TR_ARG_TUPLE(b[0], b[1], b[2], b[3]));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (!tr_address_from_string(&addr, str))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
range->end = ntohl(addr.addr.addr4.s_addr);
|
|
|
|
|
|
|
|
return true;
|
2010-05-31 11:38:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-13 21:20:40 +00:00
|
|
|
/*
|
|
|
|
* CIDR notation: "0.0.0.0/8", IPv4 only
|
|
|
|
* https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#CIDR_notation
|
|
|
|
*/
|
2019-02-10 11:05:16 +00:00
|
|
|
static bool parseLine3(char const* line, struct tr_ipv4_range* range)
|
|
|
|
{
|
2019-02-16 07:50:37 +00:00
|
|
|
unsigned int ip[4];
|
2021-11-03 03:55:43 +00:00
|
|
|
unsigned int pflen = 0;
|
|
|
|
uint32_t ip_u = 0;
|
2018-10-13 21:20:40 +00:00
|
|
|
uint32_t mask = 0xffffffff;
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if (sscanf(line, "%u.%u.%u.%u/%u", TR_ARG_TUPLE(&ip[0], &ip[1], &ip[2], &ip[3]), &pflen) != 5)
|
2019-02-10 11:05:16 +00:00
|
|
|
{
|
2018-10-13 21:20:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-10 11:05:16 +00:00
|
|
|
if (pflen > 32 || ip[0] > 0xff || ip[1] > 0xff || ip[2] > 0xff || ip[3] > 0xff)
|
|
|
|
{
|
2018-10-13 21:20:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-17 13:41:19 +00:00
|
|
|
/* this is host order */
|
2018-10-14 16:44:01 +00:00
|
|
|
mask <<= 32 - pflen;
|
2018-10-13 21:20:40 +00:00
|
|
|
ip_u = ip[0] << 24 | ip[1] << 16 | ip[2] << 8 | ip[3];
|
|
|
|
|
|
|
|
/* fill the non-prefix bits the way we need it */
|
2018-10-17 13:41:19 +00:00
|
|
|
range->begin = ip_u & mask;
|
|
|
|
range->end = ip_u | (~mask);
|
2018-10-13 21:20:40 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool parseLine(char const* line, struct tr_ipv4_range* range)
|
2010-05-31 11:38:36 +00:00
|
|
|
{
|
2018-10-13 21:20:40 +00:00
|
|
|
return parseLine1(line, range) || parseLine2(line, range) || parseLine3(line, range);
|
2010-05-31 11:38:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int compareAddressRangesByFirstAddress(void const* va, void const* vb)
|
2011-02-13 16:14:31 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto const* a = static_cast<struct tr_ipv4_range const*>(va);
|
|
|
|
auto const* b = static_cast<struct tr_ipv4_range const*>(vb);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-08 05:44:31 +00:00
|
|
|
if (a->begin < b->begin)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-08 05:44:31 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a->begin > b->begin)
|
|
|
|
{
|
|
|
|
return 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2011-02-13 16:14:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int tr_blocklistFileSetContent(tr_blocklistFile* b, char const* filename)
|
2008-03-29 18:37:07 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int inCount = 0;
|
|
|
|
char line[2048];
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* err_fmt = _("Couldn't read \"%1$s\": %2$s");
|
2021-11-03 03:55:43 +00:00
|
|
|
// TODO: should be a vector
|
2021-09-15 00:18:09 +00:00
|
|
|
struct tr_ipv4_range* ranges = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t ranges_alloc = 0;
|
|
|
|
size_t ranges_count = 0;
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_error* error = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (filename == nullptr)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
blocklistDelete(b);
|
|
|
|
return 0;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-03 03:55:43 +00:00
|
|
|
auto const in = tr_sys_file_open(filename, TR_SYS_FILE_READ, 0, &error);
|
2017-04-19 12:04:45 +00:00
|
|
|
if (in == TR_BAD_SYS_FILE)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddError(err_fmt, filename, error->message);
|
|
|
|
tr_error_free(error);
|
|
|
|
return 0;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
blocklistClose(b);
|
|
|
|
|
2021-11-03 03:55:43 +00:00
|
|
|
auto const out = tr_sys_file_open(b->filename, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0666, &error);
|
2017-04-19 12:04:45 +00:00
|
|
|
if (out == TR_BAD_SYS_FILE)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddError(err_fmt, b->filename, error->message);
|
|
|
|
tr_error_free(error);
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_sys_file_close(in, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* load the rules into memory */
|
2021-09-15 00:18:09 +00:00
|
|
|
while (tr_sys_file_read_line(in, line, sizeof(line), nullptr))
|
2008-03-29 18:37:07 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_ipv4_range range;
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
++inCount;
|
2010-04-27 03:58:25 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!parseLine(line, &range))
|
2010-04-30 22:58:03 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* don't try to display the actual lines - it causes issues */
|
|
|
|
tr_logAddError(_("blocklist skipped invalid address at line %d"), inCount);
|
|
|
|
continue;
|
2010-04-30 22:58:03 +00:00
|
|
|
}
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ranges_alloc == ranges_count)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ranges_alloc += 4096; /* arbitrary */
|
|
|
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ranges[ranges_count++] = range;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ranges_count > 0) /* sort and merge */
|
2008-06-11 16:15:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_ipv4_range* keep = ranges;
|
2011-02-13 16:14:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* sort */
|
|
|
|
qsort(ranges, ranges_count, sizeof(struct tr_ipv4_range), compareAddressRangesByFirstAddress);
|
2013-01-21 17:39:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* merge */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t i = 1; i < ranges_count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
struct tr_ipv4_range const* r = &ranges[i];
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (keep->end < r->begin)
|
|
|
|
{
|
|
|
|
*++keep = *r;
|
|
|
|
}
|
|
|
|
else if (keep->end < r->end)
|
|
|
|
{
|
|
|
|
keep->end = r->end;
|
|
|
|
}
|
|
|
|
}
|
2013-01-21 17:39:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ranges_count = keep + 1 - ranges;
|
2011-02-13 16:14:31 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
#ifdef TR_ENABLE_ASSERTS
|
2011-02-13 16:14:31 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
/* sanity checks: make sure the rules are sorted in ascending order and don't overlap */
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t i = 0; i < ranges_count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(ranges[i].begin <= ranges[i].end);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t i = 1; i < ranges_count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(ranges[i - 1].end < ranges[i].begin);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-13 16:14:31 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (!tr_sys_file_write(out, ranges, sizeof(struct tr_ipv4_range) * ranges_count, nullptr, &error))
|
2013-01-21 17:39:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddError(_("Couldn't save file \"%1$s\": %2$s"), b->filename, error->message);
|
|
|
|
tr_error_free(error);
|
2013-01-21 17:39:20 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2013-01-21 17:39:20 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
char* base = tr_sys_path_basename(b->filename, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddInfo(_("Blocklist \"%s\" updated with %zu entries"), base, ranges_count);
|
|
|
|
tr_free(base);
|
2008-06-11 16:15:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(ranges);
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_sys_file_close(out, nullptr);
|
|
|
|
tr_sys_file_close(in, nullptr);
|
2008-03-29 21:05:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
blocklistLoad(b);
|
2008-03-30 00:00:28 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ranges_count;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|