2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2010 Johannes Lieder.
|
|
|
|
// 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>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <csignal> /* sig_atomic_t */
|
|
|
|
#include <cstring> /* strlen(), strncpy(), strstr(), memset() */
|
2021-12-24 21:12:33 +00:00
|
|
|
#include <type_traits>
|
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>
|
2021-10-06 14:26:07 +00:00
|
|
|
using in_port_t = uint16_t; /* all missing */
|
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
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/event.h>
|
|
|
|
#include <event2/util.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
|
|
|
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2010-05-01 16:04:00 +00:00
|
|
|
#include "net.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "peer-mgr.h" /* tr_peerMgrAddPex() */
|
2010-05-01 16:04:00 +00:00
|
|
|
#include "session.h"
|
2022-04-02 02:55:30 +00:00
|
|
|
#include "torrent.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"
|
2010-05-01 16:04:00 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2022-04-04 18:36:48 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2022-03-16 00:51:36 +00:00
|
|
|
static auto constexpr SIZEOF_HASH_STRING = TR_SHA1_DIGEST_STRLEN;
|
2022-03-10 05:51:14 +00:00
|
|
|
|
2010-05-01 16:04:00 +00:00
|
|
|
/**
|
|
|
|
* @brief Local Peer Discovery
|
2010-05-08 08:42:45 +00:00
|
|
|
* @file tr-lpd.c
|
2010-05-01 16:04:00 +00:00
|
|
|
*
|
2010-05-08 08:42:45 +00:00
|
|
|
* This module implements the Local Peer Discovery (LPD) protocol as supported by the
|
2010-12-27 19:18:17 +00:00
|
|
|
* uTorrent client application. A typical LPD datagram is 119 bytes long.
|
2010-05-01 16:04:00 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-02-07 04:28:36 +00:00
|
|
|
static void event_callback(evutil_socket_t, short /*type*/, void* /*unused*/);
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2021-10-21 00:39:05 +00:00
|
|
|
static auto constexpr UpkeepIntervalSecs = int{ 5 };
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
static struct event* upkeep_timer = nullptr;
|
2011-03-17 13:16:23 +00:00
|
|
|
|
2015-03-18 07:34:26 +00:00
|
|
|
static tr_socket_t lpd_socket; /**<separate multicast receive socket */
|
|
|
|
static tr_socket_t lpd_socket2; /**<and multicast send socket */
|
2021-10-21 00:39:05 +00:00
|
|
|
static event* lpd_event = nullptr;
|
2010-05-08 08:42:45 +00:00
|
|
|
static tr_port lpd_port;
|
2010-05-01 16:04:00 +00:00
|
|
|
|
|
|
|
static tr_session* session;
|
|
|
|
|
2021-10-21 00:39:05 +00:00
|
|
|
static auto constexpr lpd_maxDatagramLength = int{ 200 }; /**<the size an LPD datagram must not exceed */
|
|
|
|
static char constexpr lpd_mcastGroup[] = "239.192.152.143"; /**<LPD multicast group */
|
|
|
|
static auto constexpr lpd_mcastPort = int{ 6771 }; /**<LPD source and destination UPD port */
|
|
|
|
static auto lpd_mcastAddr = sockaddr_in{}; /**<initialized from the above constants in tr_lpdInit */
|
2010-05-01 16:04:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Protocol-related information carried by a Local Peer Discovery packet */
|
2010-05-08 08:42:45 +00:00
|
|
|
struct lpd_protocolVersion
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
int major;
|
|
|
|
int minor;
|
2010-05-01 16:04:00 +00:00
|
|
|
};
|
|
|
|
|
2021-10-21 00:39:05 +00:00
|
|
|
static auto constexpr lpd_ttlSameSubnet = int{ 1 };
|
|
|
|
// static auto constexpr lpd_ttlSameSite = int{ 32 };
|
|
|
|
// static auto constexpr lpd_ttlSameRegion = int{ 64 };
|
|
|
|
// static auto constexpr lpd_ttlSameContinent = int{ 128 };
|
|
|
|
// static auto constexpr lpd_ttlUnrestricted = int{ 255 };
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2021-10-21 00:39:05 +00:00
|
|
|
static auto constexpr lpd_announceInterval = int{ 4 * 60 }; /**<4 min announce interval per torrent */
|
|
|
|
static auto constexpr lpd_announceScope = int{ lpd_ttlSameSubnet }; /**<the maximum scope for LPD datagrams */
|
2010-05-01 16:04:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @defgroup DoS Message Flood Protection
|
|
|
|
* @{
|
|
|
|
* We want to have a means to protect the libtransmission backend against message
|
|
|
|
* flooding: the strategy is to cap event processing once more than ten messages
|
|
|
|
* per second (that is, taking the average over one of our housekeeping intervals)
|
|
|
|
* got into our processing handler.
|
|
|
|
* If we'd really hit the limit and start discarding events, we either joined an
|
|
|
|
* extremely crowded multicast group or a malevolent host is sending bogus data to
|
|
|
|
* our socket. In this situation, we rather miss some announcements than blocking
|
|
|
|
* the actual task.
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup DoS
|
|
|
|
* @brief allow at most ten messages per second (interval average)
|
|
|
|
* @note this constraint is only enforced once per housekeeping interval */
|
2021-10-21 00:39:05 +00:00
|
|
|
static auto constexpr lpd_announceCapFactor = int{ 10 };
|
2010-05-01 16:04:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup DoS
|
|
|
|
* @brief number of unsolicited messages during the last HK interval
|
|
|
|
* @remark counts downwards */
|
2010-05-08 08:42:45 +00:00
|
|
|
static int lpd_unsolicitedMsgCounter;
|
2010-05-01 16:04:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @def CRLF
|
2010-05-08 08:42:45 +00:00
|
|
|
* @brief a line-feed, as understood by the LPD protocol */
|
2010-05-01 16:04:00 +00:00
|
|
|
#define CRLF "\r\n"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @defgroup HttpReqProc HTTP-style request handling
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Checks for BT-SEARCH method and separates the parameter section
|
|
|
|
* @param[in] s The request string
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] ver If non-nullptr, gets filled with protocol info from the request
|
2020-11-02 15:16:12 +00:00
|
|
|
* @return Returns a relative pointer to the beginning of the parameter section.
|
2021-10-06 16:32:17 +00:00
|
|
|
* If result is nullptr, s was invalid and no information will be returned
|
2010-05-01 16:04:00 +00:00
|
|
|
* @remark Note that the returned pointer is only usable as long as the given
|
|
|
|
* pointer s is valid; that is, return storage is temporary.
|
|
|
|
*
|
|
|
|
* Determines whether the given string checks out to be a valid BT-SEARCH message.
|
|
|
|
* If so, the return value points to the beginning of the parameter section (note:
|
|
|
|
* in this case the function returns a character sequence beginning with CRLF).
|
2021-10-06 16:32:17 +00:00
|
|
|
* If parameter is not nullptr, the declared protocol version is returned as part
|
|
|
|
* of the lpd_protocolVersion structure.
|
2010-05-01 16:04:00 +00:00
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* lpd_extractHeader(char const* s, struct lpd_protocolVersion* const ver)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(s != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-05-01 15:46:41 +00:00
|
|
|
int major = -1;
|
|
|
|
int minor = -1;
|
2022-07-27 14:03:13 +00:00
|
|
|
size_t const len = strlen(s);
|
2010-05-01 16:04:00 +00:00
|
|
|
|
|
|
|
/* something might be rotten with this chunk of data */
|
2022-02-01 17:30:51 +00:00
|
|
|
if (len == 0 || len > lpd_maxDatagramLength)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
|
|
|
/* now we can attempt to look up the BT-SEARCH header */
|
2017-04-19 12:04:45 +00:00
|
|
|
if (sscanf(s, "BT-SEARCH * HTTP/%d.%d" CRLF, &major, &minor) != 2)
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (major < 0 || minor < 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
/* a pair of blank lines at the end of the string, no place else */
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* const two_blank = CRLF CRLF CRLF;
|
|
|
|
char const* const end = strstr(s, two_blank);
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (end == nullptr || strlen(end) > strlen(two_blank))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ver != nullptr)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
|
|
|
ver->major = major;
|
|
|
|
ver->minor = minor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* separate the header, begins with CRLF */
|
2017-04-19 12:04:45 +00:00
|
|
|
return strstr(s, CRLF);
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Return the value of a named parameter
|
|
|
|
*
|
|
|
|
* @param[in] str Input string of "\r\nName: Value" pairs without HTTP-style method part
|
|
|
|
* @param[in] name Name of parameter to extract
|
|
|
|
* @param[in] n Maximum available storage for value to return
|
|
|
|
* @param[out] val Output parameter for the actual value
|
|
|
|
* @return Returns 1 if value could be copied successfully
|
|
|
|
*
|
|
|
|
* Extracts the associated value of a named parameter from a HTTP-style header by
|
|
|
|
* performing the following steps:
|
|
|
|
* - assemble search string "\r\nName: " and locate position
|
|
|
|
* - copy back value from end to next "\r\n"
|
|
|
|
*/
|
2021-10-23 15:43:15 +00:00
|
|
|
// TODO: string_view
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool lpd_extractParam(char const* const str, char const* const name, int n, char* const val)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(str != nullptr);
|
|
|
|
TR_ASSERT(name != nullptr);
|
|
|
|
TR_ASSERT(val != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2022-04-04 18:36:48 +00:00
|
|
|
auto const key = tr_strbuf<char, 30>{ CRLF, name, ": "sv };
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-04 18:36:48 +00:00
|
|
|
char const* const pos = strstr(str, key);
|
2021-09-15 00:18:09 +00:00
|
|
|
if (pos == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2015-05-31 22:13:31 +00:00
|
|
|
return false; /* search was not successful */
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
|
|
|
{
|
2022-04-04 18:36:48 +00:00
|
|
|
char const* const beg = pos + std::size(key);
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* const new_line = strstr(beg, CRLF);
|
2010-05-01 16:04:00 +00:00
|
|
|
|
|
|
|
/* the value is delimited by the next CRLF */
|
2020-11-05 22:46:21 +00:00
|
|
|
int const len = new_line - beg;
|
2010-05-01 16:04:00 +00:00
|
|
|
|
|
|
|
/* if value string hits the length limit n,
|
|
|
|
* leave space for a trailing '\0' character */
|
2021-09-19 20:41:35 +00:00
|
|
|
n = std::min(len, n - 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
strncpy(val, beg, n);
|
2010-05-01 16:04:00 +00:00
|
|
|
val[n] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we successfully returned the value string */
|
2015-05-31 22:13:31 +00:00
|
|
|
return true;
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @} */
|
|
|
|
|
2022-02-07 04:28:36 +00:00
|
|
|
static void on_upkeep_timer(evutil_socket_t, short /*unused*/, void* /*unused*/);
|
2011-03-17 13:16:23 +00:00
|
|
|
|
2010-05-01 16:04:00 +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.
|
|
|
|
*
|
2010-05-08 08:42:45 +00:00
|
|
|
* @remark Since the LPD service does not use another protocol family yet, this code is
|
2010-05-01 16:04:00 +00:00
|
|
|
* IPv4 only for the time being.
|
|
|
|
*/
|
2021-10-24 16:41:54 +00:00
|
|
|
int tr_lpdInit(tr_session* ss, tr_address* /*tr_addr*/)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2020-08-27 23:41:26 +00:00
|
|
|
/* if this check fails (i.e. the definition of hashString changed), update
|
|
|
|
* string handling in tr_lpdSendAnnounce() and tr_lpdConsiderAnnounce().
|
|
|
|
* However, the code should work as long as interfaces to the rest of
|
|
|
|
* libtransmission are compatible with char* strings. */
|
2022-01-08 23:41:05 +00:00
|
|
|
static_assert(
|
|
|
|
std::is_same_v<std::string const&, std::remove_pointer_t<decltype(std::declval<tr_torrent>().infoHashString())>>);
|
2020-08-27 23:41:26 +00:00
|
|
|
|
2010-05-01 16:04:00 +00:00
|
|
|
struct ip_mreq mcastReq;
|
2017-05-01 15:46:41 +00:00
|
|
|
int const opt_on = 1;
|
|
|
|
int const opt_off = 0;
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (session != nullptr) /* already initialized */
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
return -1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(lpd_announceInterval > 0);
|
|
|
|
TR_ASSERT(lpd_announceScope > 0);
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
lpd_port = ss->peerPort();
|
|
|
|
if (std::empty(lpd_port))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
return -1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddDebug("Initialising Local Peer Discovery");
|
2010-05-01 16:04:00 +00:00
|
|
|
|
|
|
|
/* setup datagram socket (receive) */
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
lpd_socket = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
2015-03-18 07:34:26 +00:00
|
|
|
if (lpd_socket == TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
goto fail;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2017-04-30 16:46:02 +00:00
|
|
|
if (evutil_make_socket_nonblocking(lpd_socket) == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
goto fail;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
if (setsockopt(lpd_socket, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char const*>(&opt_on), sizeof(opt_on)) == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
goto fail;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2017-05-20 16:29:23 +00:00
|
|
|
memset(&lpd_mcastAddr, 0, sizeof(lpd_mcastAddr));
|
2010-05-08 08:42:45 +00:00
|
|
|
lpd_mcastAddr.sin_family = AF_INET;
|
2017-04-19 12:04:45 +00:00
|
|
|
lpd_mcastAddr.sin_port = htons(lpd_mcastPort);
|
|
|
|
|
2017-04-30 16:46:02 +00:00
|
|
|
if (evutil_inet_pton(lpd_mcastAddr.sin_family, lpd_mcastGroup, &lpd_mcastAddr.sin_addr) == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
goto fail;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2017-05-20 16:29:23 +00:00
|
|
|
if (bind(lpd_socket, (struct sockaddr*)&lpd_mcastAddr, sizeof(lpd_mcastAddr)) == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
goto fail;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2010-05-08 08:42:45 +00:00
|
|
|
/* we want to join that LPD multicast group */
|
2017-05-20 16:29:23 +00:00
|
|
|
memset(&mcastReq, 0, sizeof(mcastReq));
|
2010-05-08 08:42:45 +00:00
|
|
|
mcastReq.imr_multiaddr = lpd_mcastAddr.sin_addr;
|
2017-04-19 12:04:45 +00:00
|
|
|
mcastReq.imr_interface.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
if (setsockopt(lpd_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, reinterpret_cast<char const*>(&mcastReq), sizeof(mcastReq)) ==
|
|
|
|
-1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
goto fail;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
if (setsockopt(lpd_socket, IPPROTO_IP, IP_MULTICAST_LOOP, reinterpret_cast<char const*>(&opt_off), sizeof(opt_off)) ==
|
|
|
|
-1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
goto fail;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* setup datagram socket (send) */
|
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
unsigned char const scope = lpd_announceScope;
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
lpd_socket2 = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
2015-03-18 07:34:26 +00:00
|
|
|
if (lpd_socket2 == TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
goto fail;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2017-04-30 16:46:02 +00:00
|
|
|
if (evutil_make_socket_nonblocking(lpd_socket2) == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
goto fail;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
|
|
|
/* configure outbound multicast TTL */
|
2021-09-12 17:41:49 +00:00
|
|
|
if (setsockopt(lpd_socket2, IPPROTO_IP, IP_MULTICAST_TTL, reinterpret_cast<char const*>(&scope), sizeof(scope)) == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
goto fail;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
if (setsockopt(lpd_socket2, IPPROTO_IP, IP_MULTICAST_LOOP, reinterpret_cast<char const*>(&opt_off), sizeof(opt_off)) ==
|
|
|
|
-1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
goto fail;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
session = ss;
|
|
|
|
|
2010-05-08 08:42:45 +00:00
|
|
|
/* Note: lpd_unsolicitedMsgCounter remains 0 until the first timeout event, thus
|
2010-05-01 16:04:00 +00:00
|
|
|
* any announcement received during the initial interval will be discarded. */
|
|
|
|
|
2022-08-08 22:53:20 +00:00
|
|
|
lpd_event = event_new(ss->eventBase(), lpd_socket, EV_READ | EV_PERSIST, event_callback, nullptr);
|
2021-09-15 00:18:09 +00:00
|
|
|
event_add(lpd_event, nullptr);
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-08 22:53:20 +00:00
|
|
|
upkeep_timer = evtimer_new(ss->eventBase(), on_upkeep_timer, ss);
|
2022-04-08 00:20:29 +00:00
|
|
|
tr_timerAdd(*upkeep_timer, UpkeepIntervalSecs, 0);
|
2011-03-17 13:16:23 +00:00
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddDebug("Local Peer Discovery initialised");
|
2010-05-01 16:04:00 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
fail:
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const save = errno;
|
2017-04-19 12:04:45 +00:00
|
|
|
evutil_closesocket(lpd_socket);
|
|
|
|
evutil_closesocket(lpd_socket2);
|
2015-03-18 07:34:26 +00:00
|
|
|
lpd_socket = lpd_socket2 = TR_BAD_SOCKET;
|
2021-09-15 00:18:09 +00:00
|
|
|
session = nullptr;
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddWarn(fmt::format(
|
|
|
|
_("Couldn't initialize LPD: {error} ({error_code})"),
|
|
|
|
fmt::arg("error", tr_strerror(save)),
|
|
|
|
fmt::arg("error_code", save)));
|
2010-05-01 16:04:00 +00:00
|
|
|
errno = save;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_lpdUninit(tr_session* ss)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (session != ss)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace("Uninitialising Local Peer Discovery");
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
event_free(lpd_event);
|
2021-09-15 00:18:09 +00:00
|
|
|
lpd_event = nullptr;
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
evtimer_del(upkeep_timer);
|
2021-09-15 00:18:09 +00:00
|
|
|
upkeep_timer = nullptr;
|
2011-03-17 13:16:23 +00:00
|
|
|
|
2010-05-01 16:04:00 +00:00
|
|
|
/* just shut down, we won't remember any former nodes */
|
2017-04-19 12:04:45 +00:00
|
|
|
evutil_closesocket(lpd_socket);
|
|
|
|
evutil_closesocket(lpd_socket2);
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace("Done uninitialising Local Peer Discovery");
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
session = nullptr;
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @endcond */
|
|
|
|
|
|
|
|
/**
|
2010-05-08 08:42:45 +00:00
|
|
|
* @defgroup LdsProto LPD announcement processing
|
2010-05-01 16:04:00 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Announce the given torrent on the local network
|
|
|
|
*
|
|
|
|
* @param[in] t Torrent to announce
|
2011-03-22 15:19:54 +00:00
|
|
|
* @return Returns true on success
|
2010-05-01 16:04:00 +00:00
|
|
|
*
|
2010-05-08 08:42:45 +00:00
|
|
|
* Send a query for torrent t out to the LPD multicast group (or the LAN, for that
|
2010-12-27 19:18:17 +00:00
|
|
|
* matter). A listening client on the same network might react by adding us to his
|
2010-05-01 16:04:00 +00:00
|
|
|
* peer pool for torrent t.
|
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_lpdSendAnnounce(tr_torrent const* t)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (t == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
return false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-04-04 18:36:48 +00:00
|
|
|
auto const query = fmt::format(
|
|
|
|
FMT_STRING("BT-SEARCH * HTTP/{:d}.{:d}" CRLF "Host: {:s}:{:d}" CRLF "Port: {:d}" CRLF "Infohash: {:s}" CRLF CRLF CRLF),
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
lpd_mcastGroup,
|
|
|
|
lpd_mcastPort,
|
2022-04-21 15:58:13 +00:00
|
|
|
lpd_port.host(),
|
2022-04-04 18:36:48 +00:00
|
|
|
tr_strupper(t->infoHashString()));
|
|
|
|
|
|
|
|
// send the query out using [lpd_socket2]
|
|
|
|
// destination address info has already been set up in tr_lpdInit(),
|
|
|
|
// so we refrain from preparing another sockaddr_in here
|
|
|
|
if (auto const res = sendto(
|
|
|
|
lpd_socket2,
|
|
|
|
std::data(query),
|
|
|
|
std::size(query),
|
|
|
|
0,
|
|
|
|
(struct sockaddr const*)&lpd_mcastAddr,
|
|
|
|
sizeof(lpd_mcastAddr));
|
|
|
|
res != static_cast<int>(std::size(query)))
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2022-04-04 18:36:48 +00:00
|
|
|
return false;
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 00:51:36 +00:00
|
|
|
tr_logAddTraceTor(t, "LPD announce message away");
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
return true;
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Process incoming unsolicited messages and add the peer to the announced
|
|
|
|
* torrent if all checks are passed.
|
|
|
|
*
|
2022-04-21 14:28:38 +00:00
|
|
|
* @param[in,out] peer Address information of the peer to add
|
2010-05-01 16:04:00 +00:00
|
|
|
* @param[in] msg The announcement message to consider
|
|
|
|
* @return Returns 0 if any input parameter or the announce was invalid, 1 if the peer
|
|
|
|
* was successfully added, -1 if not; a non-null return value indicates a side-effect to
|
|
|
|
* the peer in/out parameter.
|
|
|
|
*
|
2010-05-08 08:42:45 +00:00
|
|
|
* @note The port information gets added to the peer structure if tr_lpdConsiderAnnounce
|
2010-12-27 19:18:17 +00:00
|
|
|
* is able to extract the necessary information from the announce message. That is, if
|
2010-05-01 16:04:00 +00:00
|
|
|
* return != 0, the caller may retrieve the value from the passed structure.
|
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
static int tr_lpdConsiderAnnounce(tr_pex* peer, char const* const msg)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2021-10-21 00:39:05 +00:00
|
|
|
auto constexpr MaxValueLen = int{ 25 };
|
|
|
|
auto constexpr MaxHashLen = int{ SIZEOF_HASH_STRING };
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
auto ver = lpd_protocolVersion{ -1, -1 };
|
2021-10-21 00:39:05 +00:00
|
|
|
char value[MaxValueLen] = { 0 };
|
|
|
|
char hashString[MaxHashLen] = { 0 };
|
2017-05-01 15:46:41 +00:00
|
|
|
int res = 0;
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (peer != nullptr && msg != nullptr)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_torrent* tor = nullptr;
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* params = lpd_extractHeader(msg, &ver);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (params == nullptr || ver.major != 1) /* allow messages of protocol v1 */
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
return 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
|
|
|
/* save the effort to check Host, which seems to be optional anyway */
|
|
|
|
|
2021-10-21 00:39:05 +00:00
|
|
|
if (!lpd_extractParam(params, "Port", MaxValueLen, value))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
return 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
|
|
|
/* determine announced peer port, refuse if value too large */
|
2022-04-21 15:58:13 +00:00
|
|
|
int peer_port = 0;
|
|
|
|
if (sscanf(value, "%d", &peer_port) != 1 || peer_port > (in_port_t)-1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
return 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
peer->port.setHost(peer_port);
|
2010-05-01 16:04:00 +00:00
|
|
|
res = -1; /* signal caller side-effect to peer->port via return != 0 */
|
|
|
|
|
2021-10-21 00:39:05 +00:00
|
|
|
if (!lpd_extractParam(params, "Infohash", MaxHashLen, hashString))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
return res;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-03-01 23:06:29 +00:00
|
|
|
tor = session->torrents().get(hashString);
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2021-12-15 05:48:56 +00:00
|
|
|
if (tr_isTorrent(tor) && tor->allowsLpd())
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
|
|
|
/* we found a suitable peer, add it to the torrent */
|
2021-09-09 20:25:30 +00:00
|
|
|
tr_peerMgrAddPex(tor, TR_PEER_FROM_LPD, peer, 1);
|
2022-04-21 15:58:13 +00:00
|
|
|
tr_logAddDebugTor(
|
|
|
|
tor,
|
2022-04-21 23:37:02 +00:00
|
|
|
fmt::format(FMT_STRING("Found a local peer from LPD ({:s})"), peer->addr.readable(peer->port)));
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* periodic reconnectPulse() deals with the rest... */
|
2010-05-01 16:04:00 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2021-10-14 19:26:38 +00:00
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
tr_logAddDebug(fmt::format(FMT_STRING("Cannot serve torrent #{:s}"), hashString));
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @} */
|
|
|
|
|
|
|
|
/**
|
2010-05-08 08:42:45 +00:00
|
|
|
* @note Since it possible for tr_lpdAnnounceMore to get called from outside the LPD module,
|
2010-05-01 16:04:00 +00:00
|
|
|
* the function needs to be informed of the externally employed housekeeping interval.
|
2010-05-08 08:42:45 +00:00
|
|
|
* Further, by setting interval to zero (or negative) the caller may actually disable LPD
|
2010-05-01 16:04:00 +00:00
|
|
|
* announces on a per-interval basis.
|
2011-03-17 13:16:23 +00:00
|
|
|
*
|
2020-09-11 21:07:45 +00:00
|
|
|
* TODO: since this function's been made private and is called by a periodic timer,
|
2011-03-17 13:16:23 +00:00
|
|
|
* most of the previous paragraph isn't true anymore... we weren't using that functionality
|
|
|
|
* before. are there cases where we should? if not, should we remove the bells & whistles?
|
2010-05-01 16:04:00 +00:00
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
static int tr_lpdAnnounceMore(time_t const now, int const interval)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
|
|
|
int announcesSent = 0;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_isSession(session))
|
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
return -1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2021-09-24 23:31:02 +00:00
|
|
|
if (tr_sessionAllowsLPD(session))
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2022-03-01 23:06:29 +00:00
|
|
|
for (auto* const tor : session->torrents())
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2011-03-04 17:33:29 +00:00
|
|
|
int announcePrio = 0;
|
|
|
|
|
2021-12-15 05:48:56 +00:00
|
|
|
if (!tor->allowsLpd())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
continue;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2011-03-04 17:33:29 +00:00
|
|
|
/* issue #3208: prioritize downloads before seeds */
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (tr_torrentGetActivity(tor))
|
2011-03-04 17:33:29 +00:00
|
|
|
{
|
|
|
|
case TR_STATUS_DOWNLOAD:
|
|
|
|
announcePrio = 1;
|
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2011-03-04 17:33:29 +00:00
|
|
|
case TR_STATUS_SEED:
|
|
|
|
announcePrio = 2;
|
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-06 17:24:02 +00:00
|
|
|
default:
|
2011-03-04 17:33:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (announcePrio > 0 && tor->lpdAnnounceAt <= now)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_lpdSendAnnounce(tor))
|
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
announcesSent++;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tor->lpdAnnounceAt = now + lpd_announceInterval * announcePrio;
|
2011-03-04 17:33:29 +00:00
|
|
|
|
2010-05-01 16:04:00 +00:00
|
|
|
break; /* that's enough; for this interval */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* perform housekeeping for the flood protection mechanism */
|
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const maxAnnounceCap = interval * lpd_announceCapFactor;
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (lpd_unsolicitedMsgCounter < 0)
|
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)",
|
|
|
|
-lpd_unsolicitedMsgCounter,
|
|
|
|
maxAnnounceCap));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2010-05-08 08:42:45 +00:00
|
|
|
lpd_unsolicitedMsgCounter = maxAnnounceCap;
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return announcesSent;
|
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void on_upkeep_timer(evutil_socket_t /*s*/, short /*type*/, void* /*user_data*/)
|
2011-03-17 13:16:23 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t const now = tr_time();
|
2021-10-21 00:39:05 +00:00
|
|
|
tr_lpdAnnounceMore(now, UpkeepIntervalSecs);
|
2022-04-08 00:20:29 +00:00
|
|
|
tr_timerAdd(*upkeep_timer, UpkeepIntervalSecs, 0);
|
2011-03-17 13:16:23 +00:00
|
|
|
}
|
|
|
|
|
2010-05-01 16:04:00 +00:00
|
|
|
/**
|
|
|
|
* @brief Processing of timeout notifications and incoming data on the socket
|
2010-05-08 08:42:45 +00:00
|
|
|
* @note maximum rate of read events is limited according to @a lpd_maxAnnounceCap
|
2010-05-01 16:04:00 +00:00
|
|
|
* @see DoS */
|
2021-10-24 16:41:54 +00:00
|
|
|
static void event_callback(evutil_socket_t /*s*/, short type, void* /*user_data*/)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isSession(session));
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2010-05-08 08:42:45 +00:00
|
|
|
/* do not allow announces to be processed if LPD is disabled */
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_sessionAllowsLPD(session))
|
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((type & EV_READ) != 0)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in foreignAddr;
|
2017-05-01 15:47:49 +00:00
|
|
|
int addrLen = sizeof(foreignAddr);
|
2015-05-09 11:56:35 +00:00
|
|
|
char foreignMsg[lpd_maxDatagramLength + 1];
|
2010-05-01 16:04:00 +00:00
|
|
|
|
|
|
|
/* process local announcement from foreign peer */
|
2022-07-27 14:03:13 +00:00
|
|
|
int const res = recvfrom(
|
2021-08-15 09:41:48 +00:00
|
|
|
lpd_socket,
|
2021-09-12 17:41:49 +00:00
|
|
|
foreignMsg,
|
2021-08-15 09:41:48 +00:00
|
|
|
lpd_maxDatagramLength,
|
|
|
|
0,
|
|
|
|
(struct sockaddr*)&foreignAddr,
|
2017-04-19 12:04:45 +00:00
|
|
|
(socklen_t*)&addrLen);
|
2010-05-01 16:04:00 +00:00
|
|
|
|
|
|
|
/* besides, do we get flooded? then bail out! */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (--lpd_unsolicitedMsgCounter < 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (res > 0 && res <= lpd_maxDatagramLength)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto foreignPeer = tr_pex{};
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2015-05-09 11:56:35 +00:00
|
|
|
/* be paranoid enough about zero terminating the foreign string */
|
|
|
|
foreignMsg[res] = '\0';
|
|
|
|
|
2010-05-01 16:04:00 +00:00
|
|
|
foreignPeer.addr.addr.addr4 = foreignAddr.sin_addr;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (tr_lpdConsiderAnnounce(&foreignPeer, foreignMsg) != 0)
|
|
|
|
{
|
2010-05-01 16:04:00 +00:00
|
|
|
return; /* OK so far, no log message */
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace("Discarded invalid multicast message");
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
}
|