forked from premiere/premiere-libtorrent
created lsd_callback, log optimization and cleanup in lsd (#1099)
This commit is contained in:
parent
d94c317f02
commit
b4da884f29
|
@ -176,6 +176,7 @@ nobase_include_HEADERS = \
|
|||
aux_/cppint_import_export.hpp \
|
||||
aux_/ffs.hpp \
|
||||
aux_/portmap.hpp \
|
||||
aux_/lsd.hpp \
|
||||
\
|
||||
extensions/smart_ban.hpp \
|
||||
extensions/ut_metadata.hpp \
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
|
||||
Copyright (c) 2016, Arvid Norberg, Alden Torres
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the author nor the names of its
|
||||
contributors may be used to endorse or promote products derived
|
||||
from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef LIBTORRENT_LSD_HPP
|
||||
#define LIBTORRENT_LSD_HPP
|
||||
|
||||
#include "libtorrent/config.hpp"
|
||||
|
||||
namespace libtorrent {
|
||||
namespace aux
|
||||
{
|
||||
struct TORRENT_EXTRA_EXPORT lsd_callback
|
||||
{
|
||||
virtual void on_lsd_peer(tcp::endpoint const& peer, sha1_hash const& ih) = 0;
|
||||
#ifndef TORRENT_DISABLE_LOGGING
|
||||
virtual bool should_log_lsd() const = 0;
|
||||
virtual void log_lsd(char const* msg) const = 0;
|
||||
#endif
|
||||
|
||||
protected:
|
||||
~lsd_callback() {}
|
||||
};
|
||||
}}
|
||||
|
||||
#endif // LIBTORRENT_LSD_HPP
|
|
@ -79,6 +79,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||
#include "libtorrent/invariant_check.hpp"
|
||||
#include "libtorrent/extensions.hpp"
|
||||
#include "libtorrent/aux_/portmap.hpp"
|
||||
#include "libtorrent/aux_/lsd.hpp"
|
||||
|
||||
#if TORRENT_COMPLETE_TYPES_REQUIRED
|
||||
#include "libtorrent/peer_connection.hpp"
|
||||
|
@ -187,6 +188,7 @@ namespace libtorrent
|
|||
: session_interface
|
||||
, dht::dht_observer
|
||||
, aux::portmap_callback
|
||||
, aux::lsd_callback
|
||||
, boost::noncopyable
|
||||
, uncork_interface
|
||||
, single_threaded
|
||||
|
@ -380,9 +382,6 @@ namespace libtorrent
|
|||
#endif
|
||||
|
||||
void on_lsd_announce(error_code const& e);
|
||||
#ifndef TORRENT_DISABLE_LOGGING
|
||||
void on_lsd_log(char const* log);
|
||||
#endif
|
||||
|
||||
// called when a port mapping is successful, or a router returns
|
||||
// a failure to map a port
|
||||
|
@ -629,6 +628,9 @@ namespace libtorrent
|
|||
virtual bool should_log_portmap(aux::portmap_transport transport) const override;
|
||||
virtual void log_portmap(aux::portmap_transport transport, char const* msg)
|
||||
const override;
|
||||
|
||||
virtual bool should_log_lsd() const override;
|
||||
virtual void log_lsd(char const* msg) const override;
|
||||
#endif
|
||||
|
||||
virtual bool on_dht_request(string_view query
|
||||
|
@ -731,7 +733,7 @@ namespace libtorrent
|
|||
|
||||
void on_trigger_auto_manage();
|
||||
|
||||
void on_lsd_peer(tcp::endpoint peer, sha1_hash const& ih);
|
||||
void on_lsd_peer(tcp::endpoint const& peer, sha1_hash const& ih) override;
|
||||
void setup_socket_buffers(socket_type& s) override;
|
||||
|
||||
// the settings for the client
|
||||
|
|
|
@ -37,23 +37,15 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||
#include "libtorrent/peer_id.hpp"
|
||||
#include "libtorrent/broadcast_socket.hpp"
|
||||
#include "libtorrent/deadline_timer.hpp"
|
||||
#include "libtorrent/aux_/lsd.hpp"
|
||||
|
||||
namespace libtorrent
|
||||
{
|
||||
|
||||
typedef std::function<void(tcp::endpoint, sha1_hash)> peer_callback_t;
|
||||
#ifndef TORRENT_DISABLE_LOGGING
|
||||
typedef std::function<void(char const*)> log_callback_t;
|
||||
#endif
|
||||
|
||||
class lsd : public std::enable_shared_from_this<lsd>
|
||||
{
|
||||
public:
|
||||
lsd(io_service& ios, peer_callback_t const& cb
|
||||
#ifndef TORRENT_DISABLE_LOGGING
|
||||
, log_callback_t const& log
|
||||
#endif
|
||||
);
|
||||
lsd(io_service& ios, aux::lsd_callback& cb);
|
||||
~lsd();
|
||||
|
||||
void start(error_code& ec);
|
||||
|
@ -69,10 +61,10 @@ private:
|
|||
, bool broadcast, int retry_count);
|
||||
void resend_announce(error_code const& e, sha1_hash const& ih
|
||||
, int listen_port, int retry_count);
|
||||
void on_announce(udp::endpoint const& from, char* buffer
|
||||
void on_announce(udp::endpoint const& from, char const* buffer
|
||||
, std::size_t bytes_transferred);
|
||||
|
||||
peer_callback_t m_callback;
|
||||
aux::lsd_callback& m_callback;
|
||||
|
||||
// the udp socket used to send and receive
|
||||
// multicast messages on
|
||||
|
@ -81,7 +73,7 @@ private:
|
|||
broadcast_socket m_socket6;
|
||||
#endif
|
||||
#ifndef TORRENT_DISABLE_LOGGING
|
||||
log_callback_t m_log_cb;
|
||||
bool should_log() const;
|
||||
void debug_log(char const* fmt, ...) const TORRENT_FORMAT(2,3);
|
||||
#endif
|
||||
|
||||
|
|
77
src/lsd.cpp
77
src/lsd.cpp
|
@ -32,28 +32,17 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||
|
||||
#include <cstdlib>
|
||||
#include <cstdarg>
|
||||
#include <functional>
|
||||
|
||||
#include "libtorrent/lsd.hpp"
|
||||
#include "libtorrent/io.hpp"
|
||||
#include "libtorrent/http_tracker_connection.hpp"
|
||||
#include "libtorrent/buffer.hpp"
|
||||
#include "libtorrent/time.hpp"
|
||||
#include "libtorrent/random.hpp"
|
||||
#include "libtorrent/http_parser.hpp"
|
||||
#include "libtorrent/socket_io.hpp" // for print_address
|
||||
#include "libtorrent/debug.hpp"
|
||||
#include "libtorrent/hex.hpp" // to_hex, from_hex
|
||||
|
||||
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
||||
|
||||
#include <boost/asio/ip/host_name.hpp>
|
||||
#include <boost/asio/ip/multicast.hpp>
|
||||
|
||||
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <functional>
|
||||
#include <cstdarg>
|
||||
|
||||
using namespace std::placeholders;
|
||||
|
||||
namespace libtorrent
|
||||
|
@ -76,18 +65,11 @@ int render_lsd_packet(char* dst, int const len, int const listen_port
|
|||
|
||||
static error_code dummy;
|
||||
|
||||
lsd::lsd(io_service& ios, peer_callback_t const& cb
|
||||
#ifndef TORRENT_DISABLE_LOGGING
|
||||
, log_callback_t const& log
|
||||
#endif
|
||||
)
|
||||
lsd::lsd(io_service& ios, aux::lsd_callback& cb)
|
||||
: m_callback(cb)
|
||||
, m_socket(udp::endpoint(address_v4::from_string("239.192.152.143", dummy), 6771))
|
||||
#if TORRENT_USE_IPV6
|
||||
, m_socket6(udp::endpoint(address_v6::from_string("ff15::efc0:988f", dummy), 6771))
|
||||
#endif
|
||||
#ifndef TORRENT_DISABLE_LOGGING
|
||||
, m_log_cb(log)
|
||||
#endif
|
||||
, m_broadcast_timer(ios)
|
||||
, m_cookie((random(0x7fffffff) ^ std::uintptr_t(this)) & 0x7fffffff)
|
||||
|
@ -99,16 +81,22 @@ lsd::lsd(io_service& ios, peer_callback_t const& cb
|
|||
}
|
||||
|
||||
#ifndef TORRENT_DISABLE_LOGGING
|
||||
bool lsd::should_log() const
|
||||
{
|
||||
return m_callback.should_log_lsd();
|
||||
}
|
||||
|
||||
TORRENT_FORMAT(2,3)
|
||||
void lsd::debug_log(char const* fmt, ...) const
|
||||
{
|
||||
if (!should_log()) return;
|
||||
va_list v;
|
||||
va_start(v, fmt);
|
||||
|
||||
char buf[1024];
|
||||
vsnprintf(buf, sizeof(buf), fmt, v);
|
||||
va_end(v);
|
||||
m_log_cb(buf);
|
||||
m_callback.log_lsd(buf);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -158,8 +146,11 @@ void lsd::announce_impl(sha1_hash const& ih, int const listen_port
|
|||
{
|
||||
m_disabled = true;
|
||||
#ifndef TORRENT_DISABLE_LOGGING
|
||||
debug_log("*** LSD: failed to send message: (%d) %s", ec.value()
|
||||
, ec.message().c_str());
|
||||
if (should_log())
|
||||
{
|
||||
debug_log("*** LSD: failed to send message: (%d) %s", ec.value()
|
||||
, ec.message().c_str());
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
@ -174,8 +165,11 @@ void lsd::announce_impl(sha1_hash const& ih, int const listen_port
|
|||
{
|
||||
m_disabled6 = true;
|
||||
#ifndef TORRENT_DISABLE_LOGGING
|
||||
debug_log("*** LSD: failed to send message6: (%d) %s", ec.value()
|
||||
, ec.message().c_str());
|
||||
if (should_log())
|
||||
{
|
||||
debug_log("*** LSD: failed to send message6: (%d) %s", ec.value()
|
||||
, ec.message().c_str());
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
@ -205,11 +199,9 @@ void lsd::resend_announce(error_code const& e, sha1_hash const& info_hash
|
|||
announce_impl(info_hash, listen_port, false, retry_count);
|
||||
}
|
||||
|
||||
void lsd::on_announce(udp::endpoint const& from, char* buf
|
||||
, std::size_t bytes_transferred)
|
||||
void lsd::on_announce(udp::endpoint const& from, char const* buf
|
||||
, std::size_t const bytes_transferred)
|
||||
{
|
||||
using namespace libtorrent::detail;
|
||||
|
||||
http_parser p;
|
||||
|
||||
bool error = false;
|
||||
|
@ -240,12 +232,11 @@ void lsd::on_announce(udp::endpoint const& from, char* buf
|
|||
return;
|
||||
}
|
||||
|
||||
int port = std::atoi(port_str.c_str());
|
||||
int const port = std::atoi(port_str.c_str());
|
||||
|
||||
typedef std::multimap<std::string, std::string> headers_t;
|
||||
headers_t const& headers = p.headers();
|
||||
auto const& headers = p.headers();
|
||||
|
||||
headers_t::const_iterator cookie_iter = headers.find("cookie");
|
||||
auto const cookie_iter = headers.find("cookie");
|
||||
if (cookie_iter != headers.end())
|
||||
{
|
||||
// we expect it to be hexadecimal
|
||||
|
@ -261,10 +252,8 @@ void lsd::on_announce(udp::endpoint const& from, char* buf
|
|||
}
|
||||
}
|
||||
|
||||
std::pair<headers_t::const_iterator, headers_t::const_iterator> ihs
|
||||
= headers.equal_range("infohash");
|
||||
|
||||
for (headers_t::const_iterator i = ihs.first; i != ihs.second; ++i)
|
||||
auto const ihs = headers.equal_range("infohash");
|
||||
for (auto i = ihs.first; i != ihs.second; ++i)
|
||||
{
|
||||
std::string const& ih_str = i->second;
|
||||
if (ih_str.size() != 40)
|
||||
|
@ -282,13 +271,16 @@ void lsd::on_announce(udp::endpoint const& from, char* buf
|
|||
if (!ih.is_all_zeros() && port != 0)
|
||||
{
|
||||
#ifndef TORRENT_DISABLE_LOGGING
|
||||
debug_log("<== LSD: %s:%d ih: %s"
|
||||
, print_address(from.address()).c_str()
|
||||
, port, ih_str.c_str());
|
||||
if (should_log())
|
||||
{
|
||||
debug_log("<== LSD: %s:%d ih: %s"
|
||||
, print_address(from.address()).c_str()
|
||||
, port, ih_str.c_str());
|
||||
}
|
||||
#endif
|
||||
// we got an announce, pass it on through the callback
|
||||
TORRENT_TRY {
|
||||
m_callback(tcp::endpoint(from.address(), port), ih);
|
||||
m_callback.on_lsd_peer(tcp::endpoint(from.address(), port), ih);
|
||||
} TORRENT_CATCH(std::exception&) {}
|
||||
}
|
||||
}
|
||||
|
@ -306,7 +298,6 @@ void lsd::close()
|
|||
#if TORRENT_USE_IPV6
|
||||
m_disabled6 = true;
|
||||
#endif
|
||||
m_callback = nullptr;
|
||||
}
|
||||
|
||||
} // libtorrent namespace
|
||||
|
|
|
@ -5348,7 +5348,7 @@ namespace aux {
|
|||
m_lsd->announce(ih, port, broadcast);
|
||||
}
|
||||
|
||||
void session_impl::on_lsd_peer(tcp::endpoint peer, sha1_hash const& ih)
|
||||
void session_impl::on_lsd_peer(tcp::endpoint const& peer, sha1_hash const& ih)
|
||||
{
|
||||
m_stats_counters.inc_stats_counter(counters::on_lsd_peer_counter);
|
||||
TORRENT_ASSERT(is_single_thread());
|
||||
|
@ -6456,26 +6456,13 @@ namespace aux {
|
|||
|
||||
if (m_lsd) return;
|
||||
|
||||
m_lsd = std::make_shared<lsd>(std::ref(m_io_service)
|
||||
, std::bind(&session_impl::on_lsd_peer, this, _1, _2)
|
||||
#ifndef TORRENT_DISABLE_LOGGING
|
||||
, std::bind(&session_impl::on_lsd_log, this, _1)
|
||||
#endif
|
||||
);
|
||||
m_lsd = std::make_shared<lsd>(m_io_service, *this);
|
||||
error_code ec;
|
||||
m_lsd->start(ec);
|
||||
if (ec && m_alerts.should_post<lsd_error_alert>())
|
||||
m_alerts.emplace_alert<lsd_error_alert>(ec);
|
||||
}
|
||||
|
||||
#ifndef TORRENT_DISABLE_LOGGING
|
||||
void session_impl::on_lsd_log(char const* log)
|
||||
{
|
||||
if (!m_alerts.should_post<log_alert>()) return;
|
||||
m_alerts.emplace_alert<log_alert>(log);
|
||||
}
|
||||
#endif
|
||||
|
||||
natpmp* session_impl::start_natpmp()
|
||||
{
|
||||
INVARIANT_CHECK;
|
||||
|
@ -6484,7 +6471,7 @@ namespace aux {
|
|||
|
||||
// the natpmp constructor may fail and call the callbacks
|
||||
// into the session_impl.
|
||||
m_natpmp = std::make_shared<natpmp>(std::ref(m_io_service), *this);
|
||||
m_natpmp = std::make_shared<natpmp>(m_io_service, *this);
|
||||
m_natpmp->start();
|
||||
|
||||
for (auto& s : m_listen_sockets)
|
||||
|
@ -6501,7 +6488,7 @@ namespace aux {
|
|||
if (m_upnp) return m_upnp.get();
|
||||
|
||||
// the upnp constructor may fail and call the callbacks
|
||||
m_upnp = std::make_shared<upnp>(std::ref(m_io_service)
|
||||
m_upnp = std::make_shared<upnp>(m_io_service
|
||||
, m_settings.get_str(settings_pack::user_agent)
|
||||
, *this
|
||||
, m_settings.get_bool(settings_pack::upnp_ignore_nonrouters));
|
||||
|
@ -6658,6 +6645,17 @@ namespace aux {
|
|||
if (m_alerts.should_post<portmap_log_alert>())
|
||||
m_alerts.emplace_alert<portmap_log_alert>(map_transport, msg);
|
||||
}
|
||||
|
||||
bool session_impl::should_log_lsd() const
|
||||
{
|
||||
return m_alerts.should_post<log_alert>();
|
||||
}
|
||||
|
||||
void session_impl::log_lsd(char const* msg) const
|
||||
{
|
||||
if (m_alerts.should_post<log_alert>())
|
||||
m_alerts.emplace_alert<log_alert>(msg);
|
||||
}
|
||||
#endif
|
||||
|
||||
bool session_impl::on_dht_request(string_view query
|
||||
|
|
Loading…
Reference in New Issue