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-05-15 16:32:22 +00:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#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)
|
|
|
|
: is_enabled_(isEnabled)
|
|
|
|
, filename_(filename)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~BlocklistFile()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool exists() const
|
|
|
|
{
|
|
|
|
return tr_sys_path_exists(getFilename(), nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] char const* getFilename() const
|
|
|
|
{
|
|
|
|
return filename_.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: This function should be const, but cannot be const due to it calling ensureLoaded()
|
|
|
|
size_t getRuleCount()
|
|
|
|
{
|
|
|
|
ensureLoaded();
|
|
|
|
|
|
|
|
return rule_count_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[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-05-15 16:32:22 +00:00
|
|
|
private:
|
|
|
|
struct IPv4Range
|
|
|
|
{
|
|
|
|
uint32_t begin_ = 0;
|
|
|
|
uint32_t end_ = 0;
|
|
|
|
|
|
|
|
/// @brief Used for std::bsearch of an IPv4 address
|
|
|
|
static int compareAddressToRange(void const* va, void const* vb)
|
|
|
|
{
|
|
|
|
auto const* a = reinterpret_cast<uint32_t const*>(va);
|
|
|
|
auto const* b = reinterpret_cast<IPv4Range const*>(vb);
|
|
|
|
|
|
|
|
if (*a < b->begin_)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*a > b->end_)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void ensureLoaded();
|
|
|
|
void load();
|
|
|
|
void close();
|
|
|
|
|
|
|
|
static bool parseLine(char const* line, IPv4Range* range);
|
|
|
|
static bool compareAddressRangesByFirstAddress(IPv4Range const& a, IPv4Range const& b);
|
|
|
|
|
|
|
|
static bool parseLine1(std::string_view line, struct IPv4Range* range);
|
|
|
|
static bool parseLine2(std::string_view line, struct IPv4Range* range);
|
|
|
|
static bool parseLine3(char const* line, IPv4Range* range);
|
|
|
|
|
|
|
|
#ifdef TR_ENABLE_ASSERTS
|
|
|
|
/// @brief Sanity checks: make sure the rules are sorted in ascending order and don't overlap
|
2022-05-23 02:22:34 +00:00
|
|
|
static void assertValidRules(std::vector<IPv4Range> 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
|
|
|
bool is_enabled_;
|
|
|
|
tr_sys_file_t fd_{ TR_BAD_SYS_FILE };
|
|
|
|
size_t rule_count_ = 0;
|
|
|
|
uint64_t byte_count_ = 0;
|
|
|
|
std::string const filename_;
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2022-05-15 16:32:22 +00:00
|
|
|
/// @brief Not a container, memory mapped file
|
|
|
|
IPv4Range* rules_ = nullptr;
|
|
|
|
};
|