2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2008-2022 Mnemosyne LLC.
|
2022-08-08 18:05:39 +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
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2008-11-24 20:17:36 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 12:04:45 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2008-11-24 20:17:36 +00:00
|
|
|
#endif
|
|
|
|
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <cstddef> // for size_t
|
|
|
|
#include <cstdint>
|
2022-10-03 00:45:38 +00:00
|
|
|
#include <cstring>
|
2022-09-30 23:29:26 +00:00
|
|
|
#include <memory>
|
2022-05-15 16:32:22 +00:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
|
|
|
|
2022-09-30 17:11:52 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#else
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
|
2022-05-15 16:32:22 +00:00
|
|
|
#include "file.h" // for tr_sys_file_t
|
|
|
|
#include "tr-assert.h"
|
|
|
|
#include "tr-macros.h"
|
|
|
|
|
2008-12-02 03:41:58 +00:00
|
|
|
struct tr_address;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-05-15 16:32:22 +00:00
|
|
|
struct BlocklistFile
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Prevent moving to protect the fd_ from accidental destruction
|
|
|
|
BlocklistFile(BlocklistFile&&) = delete;
|
|
|
|
BlocklistFile(BlocklistFile const&) = delete;
|
|
|
|
BlocklistFile& operator=(BlocklistFile const&) = delete;
|
|
|
|
BlocklistFile& operator=(BlocklistFile&&) = delete;
|
|
|
|
|
|
|
|
BlocklistFile(char const* filename, bool isEnabled)
|
2022-08-27 19:05:21 +00:00
|
|
|
: filename_(filename)
|
|
|
|
, is_enabled_(isEnabled)
|
2022-05-15 16:32:22 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~BlocklistFile()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2022-08-27 19:05:21 +00:00
|
|
|
[[nodiscard]] constexpr auto& filename() const
|
2022-05-15 16:32:22 +00:00
|
|
|
{
|
2022-08-27 19:05:21 +00:00
|
|
|
return filename_;
|
2022-05-15 16:32:22 +00:00
|
|
|
}
|
|
|
|
|
2022-08-27 19:05:21 +00:00
|
|
|
[[nodiscard]] bool exists() const
|
2022-05-15 16:32:22 +00:00
|
|
|
{
|
2022-08-27 19:05:21 +00:00
|
|
|
return tr_sys_path_exists(filename_.c_str(), nullptr);
|
2022-05-15 16:32:22 +00:00
|
|
|
}
|
|
|
|
|
2022-08-27 19:05:21 +00:00
|
|
|
[[nodiscard]] size_t getRuleCount() const
|
2022-05-15 16:32:22 +00:00
|
|
|
{
|
|
|
|
ensureLoaded();
|
|
|
|
|
2022-08-27 19:05:21 +00:00
|
|
|
return std::size(rules_);
|
2022-05-15 16:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr bool isEnabled() const
|
|
|
|
{
|
|
|
|
return is_enabled_;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-05-15 16:32:22 +00:00
|
|
|
void setEnabled(bool isEnabled)
|
|
|
|
{
|
|
|
|
is_enabled_ = isEnabled;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-05-15 16:32:22 +00:00
|
|
|
bool hasAddress(tr_address const& addr);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-05-15 16:32:22 +00:00
|
|
|
/// @brief Read the file of ranges, sort and merge, write to our own file, and reload from it
|
|
|
|
size_t setContent(char const* filename);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-09-30 23:29:26 +00:00
|
|
|
static std::vector<std::unique_ptr<BlocklistFile>> loadBlocklists(std::string_view const config_dir, bool const is_enabled);
|
|
|
|
|
2022-05-15 16:32:22 +00:00
|
|
|
private:
|
2022-09-30 17:11:52 +00:00
|
|
|
struct AddressRange
|
2022-05-15 16:32:22 +00:00
|
|
|
{
|
|
|
|
uint32_t begin_ = 0;
|
|
|
|
uint32_t end_ = 0;
|
2022-09-30 17:11:52 +00:00
|
|
|
in6_addr begin6_;
|
|
|
|
in6_addr end6_;
|
2022-05-15 16:32:22 +00:00
|
|
|
|
|
|
|
/// @brief Used for std::bsearch of an IPv4 address
|
2022-09-30 17:11:52 +00:00
|
|
|
static int compareIPv4AddressToRange(void const* va, void const* vb)
|
2022-05-15 16:32:22 +00:00
|
|
|
{
|
|
|
|
auto const* a = reinterpret_cast<uint32_t const*>(va);
|
2022-09-30 17:11:52 +00:00
|
|
|
auto const* b = reinterpret_cast<AddressRange const*>(vb);
|
2022-05-15 16:32:22 +00:00
|
|
|
|
|
|
|
if (*a < b->begin_)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*a > b->end_)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2022-09-30 17:11:52 +00:00
|
|
|
|
|
|
|
/// @brief Used for std::bsearch of an IPv6 address
|
|
|
|
static int compareIPv6AddressToRange(void const* va, void const* vb)
|
|
|
|
{
|
|
|
|
auto const* a = reinterpret_cast<in6_addr const*>(va);
|
|
|
|
auto const* b = reinterpret_cast<AddressRange const*>(vb);
|
|
|
|
|
|
|
|
if (memcmp(&a->s6_addr, &b->begin6_.s6_addr, sizeof(a->s6_addr)) < 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(&a->s6_addr, &b->end6_.s6_addr, sizeof(a->s6_addr)) > 0)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2022-05-15 16:32:22 +00:00
|
|
|
};
|
|
|
|
|
2022-09-30 17:11:52 +00:00
|
|
|
void RewriteBlocklistFile() const;
|
2022-08-27 19:05:21 +00:00
|
|
|
void ensureLoaded() const;
|
2022-05-15 16:32:22 +00:00
|
|
|
void load();
|
|
|
|
void close();
|
|
|
|
|
2022-09-30 17:11:52 +00:00
|
|
|
static bool parseLine(char const* line, AddressRange* range);
|
|
|
|
static bool compareAddressRangesByFirstAddress(AddressRange const& a, AddressRange const& b);
|
2022-05-15 16:32:22 +00:00
|
|
|
|
2022-09-30 17:11:52 +00:00
|
|
|
static bool parseLine1(std::string_view line, struct AddressRange* range);
|
|
|
|
static bool parseLine2(std::string_view line, struct AddressRange* range);
|
|
|
|
static bool parseLine3(char const* line, AddressRange* range);
|
2022-05-15 16:32:22 +00:00
|
|
|
|
|
|
|
#ifdef TR_ENABLE_ASSERTS
|
|
|
|
/// @brief Sanity checks: make sure the rules are sorted in ascending order and don't overlap
|
2022-09-30 17:11:52 +00:00
|
|
|
static void assertValidRules(std::vector<AddressRange> const& ranges);
|
2022-05-15 16:32:22 +00:00
|
|
|
#endif
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-05-15 16:32:22 +00:00
|
|
|
std::string const filename_;
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2022-08-27 19:05:21 +00:00
|
|
|
bool is_enabled_ = false;
|
2022-09-30 17:11:52 +00:00
|
|
|
mutable std::vector<AddressRange> rules_;
|
2022-05-15 16:32:22 +00:00
|
|
|
};
|