2023-02-11 20:49:42 +00:00
|
|
|
// Except where noted, this file Copyright © 2010-2023 Johannes Lieder.
|
2022-01-20 18:27:56 +00:00
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
2022-08-24 21:03:30 +00:00
|
|
|
#include <array>
|
2022-08-11 17:28:37 +00:00
|
|
|
#include <chrono>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <memory>
|
2022-08-24 21:03:30 +00:00
|
|
|
#include <optional>
|
|
|
|
#include <sstream>
|
2014-12-13 15:22:39 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <ws2tcpip.h>
|
2010-06-30 21:24:36 +00:00
|
|
|
#else
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <ctime>
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <sys/types.h>
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <sys/socket.h> /* socket(), bind() */
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <netinet/in.h> /* sockaddr_in */
|
2010-06-30 21:24:36 +00:00
|
|
|
#endif
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-11-28 14:07:04 +00:00
|
|
|
#include <event2/event.h>
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
#include <fmt/format.h>
|
2022-03-16 00:51:36 +00:00
|
|
|
|
2010-05-01 16:04:00 +00:00
|
|
|
#include "transmission.h"
|
2021-12-24 21:12:33 +00:00
|
|
|
|
2022-11-25 21:04:37 +00:00
|
|
|
#include "crypto-utils.h" // for tr_rand_obj()
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2010-05-01 16:04:00 +00:00
|
|
|
#include "net.h"
|
2022-10-01 14:12:49 +00:00
|
|
|
#include "timer.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2010-05-08 08:42:45 +00:00
|
|
|
#include "tr-lpd.h"
|
2022-08-24 21:03:30 +00:00
|
|
|
#include "utils.h" // for tr_net_init()
|
2022-11-06 21:11:30 +00:00
|
|
|
#include "utils-ev.h" // for tr_net_init()
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-04-04 18:36:48 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
// Code in this namespace Copyright © 2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only), MIT (SPDX: MIT),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
namespace
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
// opaque value, allowing the sending client to filter out its
|
|
|
|
// own announces if it receives them via multicast loopback
|
|
|
|
auto makeCookie()
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
static auto constexpr Pool = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"sv;
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-11-25 21:04:37 +00:00
|
|
|
auto buf = tr_rand_obj<std::array<char, 12>>();
|
2022-08-24 21:03:30 +00:00
|
|
|
for (auto& ch : buf)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
ch = Pool[static_cast<unsigned char>(ch) % std::size(Pool)];
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
return std::string{ std::data(buf), std::size(buf) };
|
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
constexpr char const* const McastGroup = "239.192.152.143"; /**<LPD multicast group */
|
|
|
|
auto constexpr McastPort = tr_port::fromHost(6771); /**<LPD source and destination UPD port */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A LSD announce is formatted as follows:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* BT-SEARCH * HTTP/1.1\r\n
|
|
|
|
* Host: <host>\r\n
|
|
|
|
* Port: <port>\r\n
|
|
|
|
* Infohash: <ihash>\r\n
|
|
|
|
* cookie: <cookie (optional)>\r\n
|
|
|
|
* \r\n
|
|
|
|
* \r\n
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* An announce may contain multiple, consecutive Infohash headers
|
|
|
|
* to announce the participation in more than one torrent. This
|
|
|
|
* may not be supported by older implementations. When sending
|
|
|
|
* multiple infohashes the packet length should not exceed 1400
|
|
|
|
* bytes to avoid MTU/fragmentation problems.
|
|
|
|
*/
|
|
|
|
auto makeAnnounceMsg(std::string_view cookie, tr_port port, std::string_view const* info_hash_strings, size_t n_strings)
|
|
|
|
{
|
|
|
|
static auto constexpr Major = 1;
|
|
|
|
static auto constexpr Minor = 1;
|
|
|
|
static auto constexpr CrLf = "\r\n"sv;
|
|
|
|
|
|
|
|
auto ostr = std::ostringstream{};
|
|
|
|
ostr << "BT-SEARCH * HTTP/" << Major << '.' << Minor << CrLf //
|
|
|
|
<< "Host: " << McastGroup << ':' << McastPort.host() << CrLf //
|
|
|
|
<< "Port: " << port.host() << CrLf;
|
|
|
|
for (size_t i = 0; i < n_strings; ++i)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
ostr << "Infohash: " << tr_strupper(info_hash_strings[i]) << CrLf;
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
if (!std::empty(cookie))
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2022-08-25 03:18:58 +00:00
|
|
|
ostr << "cookie: " << cookie << CrLf;
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 03:18:58 +00:00
|
|
|
ostr << CrLf << CrLf;
|
2022-08-24 21:03:30 +00:00
|
|
|
return ostr.str();
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
struct ParsedAnnounce
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
int major;
|
|
|
|
int minor;
|
|
|
|
tr_port port;
|
|
|
|
std::vector<std::string_view> info_hash_strings;
|
|
|
|
std::string_view cookie;
|
|
|
|
};
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
std::optional<ParsedAnnounce> parseAnnounceMsg(std::string_view announce)
|
|
|
|
{
|
|
|
|
static auto constexpr CrLf = "\r\n"sv;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
auto ret = ParsedAnnounce{};
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
// get major, minor
|
|
|
|
auto key = "BT-SEARCH * HTTP/"sv;
|
|
|
|
if (auto const pos = announce.find(key); pos != std::string_view::npos)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
// parse `${major}.${minor}`
|
|
|
|
auto walk = announce.substr(pos + std::size(key));
|
2022-09-02 06:04:09 +00:00
|
|
|
if (auto const major = tr_parseNum<int>(walk, &walk); major && tr_strvStartsWith(walk, '.'))
|
2022-08-24 21:03:30 +00:00
|
|
|
{
|
|
|
|
ret.major = *major;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
walk.remove_prefix(1); // the '.' between major and minor
|
2022-09-02 06:04:09 +00:00
|
|
|
if (auto const minor = tr_parseNum<int>(walk, &walk); minor && tr_strvStartsWith(walk, CrLf))
|
2022-08-24 21:03:30 +00:00
|
|
|
{
|
|
|
|
ret.minor = *minor;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
key = "Port: "sv;
|
|
|
|
if (auto const pos = announce.find(key); pos != std::string_view::npos)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
auto walk = announce.substr(pos + std::size(key));
|
2022-09-02 06:04:09 +00:00
|
|
|
if (auto const port = tr_parseNum<uint16_t>(walk, &walk); port && tr_strvStartsWith(walk, CrLf))
|
2022-08-24 21:03:30 +00:00
|
|
|
{
|
|
|
|
ret.port = tr_port::fromHost(*port);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
key = "cookie: "sv;
|
|
|
|
if (auto const pos = announce.find(key); pos != std::string_view::npos)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
auto walk = announce.substr(pos + std::size(key));
|
|
|
|
if (auto const end = walk.find(CrLf); end != std::string_view::npos)
|
|
|
|
{
|
|
|
|
ret.cookie = walk.substr(0, end);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
key = "Infohash: "sv;
|
|
|
|
for (;;)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
if (auto const pos = announce.find(key); pos != std::string_view::npos)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
announce.remove_prefix(pos + std::size(key));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-08-24 21:03:30 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
if (auto const end = announce.find(CrLf); end != std::string_view::npos)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
ret.info_hash_strings.push_back(announce.substr(0, end));
|
|
|
|
announce.remove_prefix(end + std::size(CrLf));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-08-24 21:03:30 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-08-24 21:03:30 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
} // namespace
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
class tr_lpd_impl final : public tr_lpd
|
|
|
|
{
|
|
|
|
public:
|
2022-10-01 14:12:49 +00:00
|
|
|
tr_lpd_impl(Mediator& mediator, struct event_base* event_base)
|
2022-08-24 21:03:30 +00:00
|
|
|
: mediator_{ mediator }
|
2022-10-01 14:12:49 +00:00
|
|
|
, announce_timer_{ mediator.timerMaker().create([this]() { announceUpkeep(); }) }
|
|
|
|
, dos_timer_{ mediator.timerMaker().create([this]() { dosUpkeep(); }) }
|
2022-08-24 21:03:30 +00:00
|
|
|
{
|
|
|
|
if (!init(event_base))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
announce_timer_->startRepeating(AnnounceInterval);
|
|
|
|
announceUpkeep();
|
|
|
|
dos_timer_->startRepeating(DosInterval);
|
|
|
|
dosUpkeep();
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
tr_lpd_impl(tr_lpd_impl&&) = delete;
|
|
|
|
tr_lpd_impl(tr_lpd_impl const&) = delete;
|
|
|
|
tr_lpd_impl& operator=(tr_lpd_impl&&) = delete;
|
|
|
|
tr_lpd_impl& operator=(tr_lpd_impl const&) = delete;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
~tr_lpd_impl() override
|
|
|
|
{
|
2022-11-06 21:11:30 +00:00
|
|
|
event_.reset();
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
if (mcast_rcv_socket_ != TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
evutil_closesocket(mcast_rcv_socket_);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
if (mcast_snd_socket_ != TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
evutil_closesocket(mcast_snd_socket_);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
tr_logAddTrace("Done uninitialising Local Peer Discovery");
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool init(struct event_base* event_base)
|
|
|
|
{
|
|
|
|
if (initImpl(event_base))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
return true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-08-24 21:03:30 +00:00
|
|
|
|
|
|
|
auto const err = sockerrno;
|
|
|
|
evutil_closesocket(mcast_rcv_socket_);
|
|
|
|
evutil_closesocket(mcast_snd_socket_);
|
|
|
|
mcast_rcv_socket_ = TR_BAD_SOCKET;
|
|
|
|
mcast_snd_socket_ = TR_BAD_SOCKET;
|
|
|
|
tr_logAddWarn(fmt::format(
|
|
|
|
_("Couldn't initialize LPD: {error} ({error_code})"),
|
|
|
|
fmt::arg("error", tr_strerror(err)),
|
|
|
|
fmt::arg("error_code", err)));
|
|
|
|
return false;
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
/**
|
|
|
|
* @brief Initializes Local Peer Discovery for this node
|
|
|
|
*
|
|
|
|
* For the most part, this means setting up an appropriately configured multicast socket
|
|
|
|
* and event-based message handling.
|
|
|
|
*
|
|
|
|
* @remark Since the LPD service does not use another protocol family yet, this code is
|
|
|
|
* IPv4 only for the time being.
|
|
|
|
*/
|
|
|
|
bool initImpl(struct event_base* event_base)
|
|
|
|
{
|
|
|
|
tr_net_init();
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
int const opt_on = 1;
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
static_assert(AnnounceScope > 0);
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
tr_logAddDebug("Initialising Local Peer Discovery");
|
2011-03-17 13:16:23 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
/* setup datagram socket (receive) */
|
|
|
|
{
|
|
|
|
mcast_rcv_socket_ = socket(PF_INET, SOCK_DGRAM, 0);
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
if (mcast_rcv_socket_ == TR_BAD_SOCKET)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
if (evutil_make_socket_nonblocking(mcast_rcv_socket_) == -1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
if (setsockopt(
|
|
|
|
mcast_rcv_socket_,
|
|
|
|
SOL_SOCKET,
|
|
|
|
SO_REUSEADDR,
|
|
|
|
reinterpret_cast<char const*>(&opt_on),
|
|
|
|
sizeof(opt_on)) == -1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
#if HAVE_SO_REUSEPORT
|
|
|
|
if (setsockopt(
|
|
|
|
mcast_rcv_socket_,
|
|
|
|
SOL_SOCKET,
|
|
|
|
SO_REUSEPORT,
|
|
|
|
reinterpret_cast<char const*>(&opt_on),
|
|
|
|
sizeof(opt_on)) == -1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
mcast_addr_ = {};
|
|
|
|
mcast_addr_.sin_family = AF_INET;
|
|
|
|
mcast_addr_.sin_port = McastPort.network();
|
|
|
|
mcast_addr_.sin_addr.s_addr = INADDR_ANY;
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2023-01-28 21:26:23 +00:00
|
|
|
if (bind(mcast_rcv_socket_, reinterpret_cast<sockaddr*>(&mcast_addr_), sizeof(mcast_addr_)) == -1)
|
2022-08-24 21:03:30 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
if (evutil_inet_pton(mcast_addr_.sin_family, McastGroup, &mcast_addr_.sin_addr) == -1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2011-03-17 13:16:23 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
/* we want to join that LPD multicast group */
|
2022-09-07 16:04:28 +00:00
|
|
|
struct ip_mreq mcast_req = {};
|
|
|
|
mcast_req.imr_multiaddr = mcast_addr_.sin_addr;
|
|
|
|
mcast_req.imr_interface.s_addr = INADDR_ANY;
|
2022-08-24 21:03:30 +00:00
|
|
|
|
|
|
|
if (setsockopt(
|
|
|
|
mcast_rcv_socket_,
|
|
|
|
IPPROTO_IP,
|
|
|
|
IP_ADD_MEMBERSHIP,
|
2022-09-07 16:04:28 +00:00
|
|
|
reinterpret_cast<char const*>(&mcast_req),
|
2022-08-24 21:03:30 +00:00
|
|
|
sizeof(struct ip_mreq)) == -1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
/* setup datagram socket (send) */
|
|
|
|
{
|
|
|
|
unsigned char const scope = AnnounceScope;
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
mcast_snd_socket_ = socket(PF_INET, SOCK_DGRAM, 0);
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
if (mcast_snd_socket_ == TR_BAD_SOCKET)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
if (evutil_make_socket_nonblocking(mcast_snd_socket_) == -1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
/* configure outbound multicast TTL */
|
|
|
|
if (setsockopt(
|
|
|
|
mcast_snd_socket_,
|
|
|
|
IPPROTO_IP,
|
|
|
|
IP_MULTICAST_TTL,
|
|
|
|
reinterpret_cast<char const*>(&scope),
|
|
|
|
sizeof(scope)) == -1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
/* Note: lpd_unsolicitedMsgCounter remains 0 until the first timeout event, thus
|
|
|
|
* any announcement received during the initial interval will be discarded. */
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-11-06 21:11:30 +00:00
|
|
|
event_.reset(event_new(event_base, mcast_rcv_socket_, EV_READ | EV_PERSIST, event_callback, this));
|
|
|
|
event_add(event_.get(), nullptr);
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
tr_logAddDebug("Local Peer Discovery initialised");
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
/**
|
|
|
|
* @brief Processing of timeout notifications and incoming data on the socket
|
|
|
|
* @note maximum rate of read events is limited according to @a lpd_maxAnnounceCap
|
|
|
|
* @see DoS */
|
|
|
|
static void event_callback(evutil_socket_t /*s*/, short type, void* vself)
|
|
|
|
{
|
|
|
|
if ((type & EV_READ) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
static_cast<tr_lpd_impl*>(vself)->onCanRead();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-08-24 21:03:30 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
void onCanRead()
|
|
|
|
{
|
|
|
|
if (!mediator_.allowsLPD())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
// process announcement from foreign peer
|
2022-09-23 05:51:15 +00:00
|
|
|
struct sockaddr_in foreign_addr = {};
|
2023-01-28 21:26:23 +00:00
|
|
|
auto addr_len = socklen_t{ sizeof(foreign_addr) };
|
2022-08-24 21:03:30 +00:00
|
|
|
auto foreign_msg = std::array<char, MaxDatagramLength>{};
|
|
|
|
auto const res = recvfrom(
|
|
|
|
mcast_rcv_socket_,
|
|
|
|
std::data(foreign_msg),
|
|
|
|
MaxDatagramLength,
|
|
|
|
0,
|
2023-01-28 21:26:23 +00:00
|
|
|
reinterpret_cast<sockaddr*>(&foreign_addr),
|
|
|
|
&addr_len);
|
2022-08-24 21:03:30 +00:00
|
|
|
|
|
|
|
// If we couldn't read it or it was too big, discard it
|
|
|
|
if (res < 1 || static_cast<size_t>(res) > MaxDatagramLength)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
// If it doesn't look like a BEP14 message, discard it
|
|
|
|
auto const msg = std::string_view{ std::data(foreign_msg), static_cast<size_t>(res) };
|
2022-11-29 03:03:28 +00:00
|
|
|
if (static auto constexpr SearchKey = "BT-SEARCH * HTTP/"sv; msg.find(SearchKey) == std::string_view::npos)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
// If we're receiving too many, discard it
|
|
|
|
if (++messages_received_since_upkeep_ > MaxIncomingPerUpkeep)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
return;
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
2021-10-14 19:26:38 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
// If it's an invalid message or the wrong protocol version, discard it.
|
|
|
|
// Note this comes *after* incrementing the count since there is some
|
|
|
|
// small CPU overhead in parsing, so don't do it for *every* message
|
|
|
|
auto const parsed = parseAnnounceMsg(msg);
|
|
|
|
if (!parsed || parsed->major != 1 || parsed->minor < 1 || parsed->cookie == cookie_)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
tr_logAddTrace("Discarded invalid multicast message");
|
|
|
|
return;
|
|
|
|
}
|
2011-03-04 17:33:29 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
auto peer_addr = tr_address{};
|
|
|
|
peer_addr.addr.addr4 = foreign_addr.sin_addr;
|
|
|
|
for (auto const& hash_string : parsed->info_hash_strings)
|
|
|
|
{
|
|
|
|
if (!mediator_.onPeerFound(hash_string, peer_addr, parsed->port))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
tr_logAddDebug(fmt::format(FMT_STRING("Cannot serve torrent #{:s}"), hash_string));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-08-24 21:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
void announceUpkeep()
|
|
|
|
{
|
|
|
|
if (!mediator_.allowsLPD())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
auto torrents = mediator_.torrents();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
// remove torrents that don't need to be announced
|
|
|
|
auto const now = tr_time();
|
|
|
|
auto const needs_announce = [&now](auto& info)
|
|
|
|
{
|
|
|
|
return info.allows_lpd && (info.activity == TR_STATUS_DOWNLOAD || info.activity == TR_STATUS_SEED) &&
|
|
|
|
(info.announce_after < now);
|
|
|
|
};
|
|
|
|
torrents.erase(
|
|
|
|
std::remove_if(std::begin(torrents), std::end(torrents), std::not_fn(needs_announce)),
|
|
|
|
std::end(torrents));
|
|
|
|
|
|
|
|
if (std::empty(torrents))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-03-04 17:33:29 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
// prioritize the remaining torrents
|
|
|
|
std::sort(
|
|
|
|
std::begin(torrents),
|
|
|
|
std::end(torrents),
|
|
|
|
[](auto const& a, auto const& b)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
if (a.activity != b.activity)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
return a.activity < b.activity;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
if (a.announce_after != b.announce_after)
|
|
|
|
{
|
|
|
|
return a.announce_after < b.announce_after;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
// cram in as many as will fit in a message
|
|
|
|
auto const baseline_size = std::size(makeAnnounceMsg(cookie_, mediator_.port(), nullptr, 0));
|
|
|
|
auto const size_with_one = std::size(makeAnnounceMsg(cookie_, mediator_.port(), &torrents.front().info_hash_str, 1));
|
|
|
|
auto const size_per_hash = size_with_one - baseline_size;
|
|
|
|
auto const max_torrents_per_announce = (MaxDatagramLength - baseline_size) / size_per_hash;
|
|
|
|
auto info_hash_strings = std::vector<std::string_view>{};
|
|
|
|
info_hash_strings.resize(std::min(std::size(torrents), max_torrents_per_announce));
|
|
|
|
std::transform(
|
|
|
|
std::begin(torrents),
|
|
|
|
std::begin(torrents) + std::size(info_hash_strings),
|
|
|
|
std::begin(info_hash_strings),
|
|
|
|
[](auto const& tor) { return tor.info_hash_str; });
|
|
|
|
|
|
|
|
if (!sendAnnounce(std::data(info_hash_strings), std::size(info_hash_strings)))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-03-04 17:33:29 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
auto const next_announce_after = now + TorrentAnnounceIntervalSec;
|
2022-11-28 15:45:39 +00:00
|
|
|
for (auto const& info_hash_string : info_hash_strings)
|
2022-08-24 21:03:30 +00:00
|
|
|
{
|
|
|
|
mediator_.setNextAnnounceTime(info_hash_string, next_announce_after);
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
void dosUpkeep()
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
if (messages_received_since_upkeep_ > MaxIncomingPerUpkeep)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace(fmt::format(
|
|
|
|
"Dropped {} announces in the last interval (max. {} allowed)",
|
2022-08-24 21:03:30 +00:00
|
|
|
messages_received_since_upkeep_ - MaxIncomingPerUpkeep,
|
|
|
|
MaxIncomingPerUpkeep));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
messages_received_since_upkeep_ = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
/**
|
|
|
|
* @brief Announce the given torrent on the local network
|
|
|
|
*
|
|
|
|
* @return Returns a success flag
|
|
|
|
*
|
|
|
|
* Send a query for torrent t out to the LPD multicast group (or the LAN, for that
|
|
|
|
* matter). A listening client on the same network might react by adding us to his
|
|
|
|
* peer pool for torrent t.
|
|
|
|
*/
|
|
|
|
bool sendAnnounce(std::string_view const* info_hash_strings, size_t n_strings)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
auto const announce = makeAnnounceMsg(cookie_, mediator_.port(), info_hash_strings, n_strings);
|
|
|
|
TR_ASSERT(std::size(announce) <= MaxDatagramLength);
|
|
|
|
auto const res = sendto(
|
|
|
|
mcast_snd_socket_,
|
|
|
|
std::data(announce),
|
|
|
|
std::size(announce),
|
2021-08-15 09:41:48 +00:00
|
|
|
0,
|
2023-01-28 21:26:23 +00:00
|
|
|
reinterpret_cast<sockaddr const*>(&mcast_addr_),
|
2022-08-24 21:03:30 +00:00
|
|
|
sizeof(mcast_addr_));
|
|
|
|
auto const sent = res == static_cast<int>(std::size(announce));
|
|
|
|
return sent;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
std::string const cookie_ = makeCookie();
|
|
|
|
Mediator& mediator_;
|
|
|
|
tr_socket_t mcast_rcv_socket_ = TR_BAD_SOCKET; /**<separate multicast receive socket */
|
|
|
|
tr_socket_t mcast_snd_socket_ = TR_BAD_SOCKET; /**<and multicast send socket */
|
2022-11-06 21:11:30 +00:00
|
|
|
libtransmission::evhelpers::event_unique_ptr event_;
|
2022-08-24 21:03:30 +00:00
|
|
|
|
|
|
|
static auto constexpr MaxDatagramLength = size_t{ 1400 };
|
|
|
|
sockaddr_in mcast_addr_ = {}; /**<initialized from the above constants in init() */
|
|
|
|
|
|
|
|
// BEP14: "To avoid causing multicast storms on large networks a
|
|
|
|
// client should send no more than 1 announce per minute."
|
|
|
|
static auto constexpr AnnounceInterval = 1min;
|
|
|
|
std::unique_ptr<libtransmission::Timer> announce_timer_;
|
|
|
|
|
|
|
|
// Flood Protection:
|
|
|
|
// To protect against message flooding, stop processing search messages
|
|
|
|
// after processing N per upkeep. If we hit that limit, we're either
|
|
|
|
// in a *very* crowded multicast group or a hostile host is sending us
|
|
|
|
// bogus data. Better to drop a few packets than get DoS'ed.
|
|
|
|
static auto constexpr DosInterval = 5s;
|
|
|
|
std::unique_ptr<libtransmission::Timer> dos_timer_;
|
|
|
|
static auto constexpr MaxIncomingPerSecond = int{ 10 };
|
|
|
|
static auto constexpr MaxIncomingPerUpkeep = std::chrono::duration_cast<std::chrono::seconds>(DosInterval).count() *
|
|
|
|
MaxIncomingPerSecond;
|
|
|
|
// @brief throw away messages after this number exceeds MaxIncomingPerUpkeep
|
|
|
|
size_t messages_received_since_upkeep_ = 0U;
|
|
|
|
|
|
|
|
static auto constexpr TorrentAnnounceIntervalSec = time_t{ 240U }; // how frequently to reannounce the same torrent
|
|
|
|
static auto constexpr TtlSameSubnet = int{ 1 };
|
|
|
|
static auto constexpr AnnounceScope = int{ TtlSameSubnet }; /**<the maximum scope for LPD datagrams */
|
|
|
|
};
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-10-01 14:12:49 +00:00
|
|
|
std::unique_ptr<tr_lpd> tr_lpd::create(Mediator& mediator, struct event_base* event_base)
|
2022-08-24 21:03:30 +00:00
|
|
|
{
|
2022-10-01 14:12:49 +00:00
|
|
|
return std::make_unique<tr_lpd_impl>(mediator, event_base);
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|