2007-04-04 21:11:19 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2007, Arvid Norberg
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libtorrent/pch.hpp"
|
|
|
|
|
|
|
|
#include "libtorrent/lsd.hpp"
|
|
|
|
#include "libtorrent/io.hpp"
|
|
|
|
#include "libtorrent/http_tracker_connection.hpp"
|
2008-01-31 09:24:01 +01:00
|
|
|
#include "libtorrent/buffer.hpp"
|
|
|
|
#include "libtorrent/http_parser.hpp"
|
2007-09-10 01:52:34 +02:00
|
|
|
|
2007-04-04 21:11:19 +02:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <boost/ref.hpp>
|
2008-05-20 08:03:46 +02:00
|
|
|
#if BOOST_VERSION < 103500
|
|
|
|
#include <asio/ip/host_name.hpp>
|
|
|
|
#include <asio/ip/multicast.hpp>
|
|
|
|
#else
|
2008-05-03 18:05:42 +02:00
|
|
|
#include <boost/asio/ip/host_name.hpp>
|
|
|
|
#include <boost/asio/ip/multicast.hpp>
|
2008-05-20 08:03:46 +02:00
|
|
|
#endif
|
2007-04-04 21:11:19 +02:00
|
|
|
#include <boost/thread/mutex.hpp>
|
|
|
|
#include <cstdlib>
|
2007-05-17 04:54:13 +02:00
|
|
|
#include <boost/config.hpp>
|
2007-04-04 21:11:19 +02:00
|
|
|
|
|
|
|
using boost::bind;
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
2007-05-17 04:54:13 +02:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
2007-10-01 19:21:19 +02:00
|
|
|
// defined in broadcast_socket.cpp
|
2008-05-03 18:05:42 +02:00
|
|
|
address guess_local_address(io_service&);
|
2007-05-17 04:54:13 +02:00
|
|
|
}
|
|
|
|
|
2008-11-05 06:39:18 +01:00
|
|
|
static error_code ec;
|
|
|
|
|
2007-04-04 21:11:19 +02:00
|
|
|
lsd::lsd(io_service& ios, address const& listen_interface
|
|
|
|
, peer_callback_t const& cb)
|
|
|
|
: m_callback(cb)
|
2008-03-26 22:40:58 +01:00
|
|
|
, m_retry_count(1)
|
2008-11-05 06:39:18 +01:00
|
|
|
, m_socket(ios, udp::endpoint(address_v4::from_string("239.192.152.143", ec), 6771)
|
2007-09-29 23:31:51 +02:00
|
|
|
, bind(&lsd::on_announce, self(), _1, _2, _3))
|
2007-04-04 21:11:19 +02:00
|
|
|
, m_broadcast_timer(ios)
|
|
|
|
, m_disabled(false)
|
|
|
|
{
|
|
|
|
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
|
|
|
|
m_log.open("lsd.log", std::ios::in | std::ios::out | std::ios::trunc);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
lsd::~lsd() {}
|
|
|
|
|
|
|
|
void lsd::announce(sha1_hash const& ih, int listen_port)
|
|
|
|
{
|
|
|
|
if (m_disabled) return;
|
|
|
|
|
|
|
|
std::stringstream btsearch;
|
|
|
|
btsearch << "BT-SEARCH * HTTP/1.1\r\n"
|
|
|
|
"Host: 239.192.152.143:6771\r\n"
|
|
|
|
"Port: " << listen_port << "\r\n"
|
|
|
|
"Infohash: " << ih << "\r\n"
|
|
|
|
"\r\n\r\n";
|
|
|
|
std::string const& msg = btsearch.str();
|
|
|
|
|
2008-03-26 22:40:58 +01:00
|
|
|
m_retry_count = 1;
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2007-09-10 01:52:34 +02:00
|
|
|
m_socket.send(msg.c_str(), int(msg.size()), ec);
|
2007-05-12 20:53:37 +02:00
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
m_disabled = true;
|
|
|
|
return;
|
|
|
|
}
|
2007-04-04 21:11:19 +02:00
|
|
|
|
|
|
|
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
|
2007-04-10 09:52:58 +02:00
|
|
|
m_log << time_now_string()
|
2007-04-04 21:11:19 +02:00
|
|
|
<< " ==> announce: ih: " << ih << " port: " << listen_port << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2007-12-30 00:30:39 +01:00
|
|
|
m_broadcast_timer.expires_from_now(milliseconds(250 * m_retry_count), ec);
|
2007-09-29 23:31:51 +02:00
|
|
|
m_broadcast_timer.async_wait(bind(&lsd::resend_announce, self(), _1, msg));
|
2007-04-04 21:11:19 +02:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void lsd::resend_announce(error_code const& e, std::string msg)
|
2007-04-04 21:11:19 +02:00
|
|
|
{
|
|
|
|
if (e) return;
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2007-09-10 01:52:34 +02:00
|
|
|
m_socket.send(msg.c_str(), int(msg.size()), ec);
|
2007-04-04 21:11:19 +02:00
|
|
|
|
|
|
|
++m_retry_count;
|
|
|
|
if (m_retry_count >= 5)
|
|
|
|
return;
|
|
|
|
|
2007-12-30 00:30:39 +01:00
|
|
|
m_broadcast_timer.expires_from_now(milliseconds(250 * m_retry_count), ec);
|
2007-09-29 23:31:51 +02:00
|
|
|
m_broadcast_timer.async_wait(bind(&lsd::resend_announce, self(), _1, msg));
|
2007-04-04 21:11:19 +02:00
|
|
|
}
|
|
|
|
|
2007-09-10 01:52:34 +02:00
|
|
|
void lsd::on_announce(udp::endpoint const& from, char* buffer
|
2007-04-04 21:11:19 +02:00
|
|
|
, std::size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
using namespace libtorrent::detail;
|
|
|
|
|
2007-09-25 05:14:05 +02:00
|
|
|
http_parser p;
|
|
|
|
|
2007-12-29 19:24:50 +01:00
|
|
|
bool error = false;
|
|
|
|
p.incoming(buffer::const_interval(buffer, buffer + bytes_transferred)
|
|
|
|
, error);
|
2007-09-25 05:14:05 +02:00
|
|
|
|
2007-12-29 19:24:50 +01:00
|
|
|
if (!p.header_finished() || error)
|
2007-09-25 05:14:05 +02:00
|
|
|
{
|
2007-05-17 02:01:51 +02:00
|
|
|
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
|
|
|
|
m_log << time_now_string()
|
2007-09-25 05:14:05 +02:00
|
|
|
<< " <== announce: incomplete HTTP message\n";
|
2007-05-17 02:01:51 +02:00
|
|
|
#endif
|
2007-09-25 05:14:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p.method() != "bt-search")
|
2007-04-04 21:11:19 +02:00
|
|
|
{
|
|
|
|
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
|
2007-09-25 05:14:05 +02:00
|
|
|
m_log << time_now_string()
|
|
|
|
<< " <== announce: invalid HTTP method: " << p.method() << std::endl;
|
2007-04-04 21:11:19 +02:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2007-09-25 05:14:05 +02:00
|
|
|
|
|
|
|
std::string const& port_str = p.header("port");
|
|
|
|
if (port_str.empty())
|
2007-04-04 21:11:19 +02:00
|
|
|
{
|
2007-09-25 05:14:05 +02:00
|
|
|
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
|
|
|
|
m_log << time_now_string()
|
|
|
|
<< " <== announce: invalid BT-SEARCH, missing port" << std::endl;
|
|
|
|
#endif
|
|
|
|
return;
|
2007-04-04 21:11:19 +02:00
|
|
|
}
|
|
|
|
|
2007-09-25 05:14:05 +02:00
|
|
|
std::string const& ih_str = p.header("infohash");
|
|
|
|
if (ih_str.empty())
|
|
|
|
{
|
|
|
|
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
|
|
|
|
m_log << time_now_string()
|
|
|
|
<< " <== announce: invalid BT-SEARCH, missing infohash" << std::endl;
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sha1_hash ih(0);
|
|
|
|
std::istringstream ih_sstr(ih_str);
|
|
|
|
ih_sstr >> ih;
|
2009-01-27 09:24:48 +01:00
|
|
|
int port = std::atoi(port_str.c_str());
|
2007-09-25 05:14:05 +02:00
|
|
|
|
2007-04-04 21:11:19 +02:00
|
|
|
if (!ih.is_all_zeros() && port != 0)
|
|
|
|
{
|
|
|
|
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
|
2007-04-10 09:52:58 +02:00
|
|
|
m_log << time_now_string()
|
2007-09-10 01:52:34 +02:00
|
|
|
<< " *** incoming local announce " << from.address()
|
2007-04-04 21:11:19 +02:00
|
|
|
<< ":" << port << " ih: " << ih << std::endl;
|
|
|
|
#endif
|
|
|
|
// we got an announce, pass it on through the callback
|
2007-12-30 00:30:39 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
try {
|
|
|
|
#endif
|
|
|
|
m_callback(tcp::endpoint(from.address(), port), ih);
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
}
|
2007-04-04 21:11:19 +02:00
|
|
|
catch (std::exception&) {}
|
2007-12-30 00:30:39 +01:00
|
|
|
#endif
|
2007-04-04 21:11:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void lsd::close()
|
|
|
|
{
|
2007-10-26 03:28:42 +02:00
|
|
|
m_socket.close();
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2007-12-30 00:30:39 +01:00
|
|
|
m_broadcast_timer.cancel(ec);
|
2007-11-18 05:11:47 +01:00
|
|
|
m_disabled = true;
|
|
|
|
m_callback.clear();
|
2007-04-04 21:11:19 +02:00
|
|
|
}
|
|
|
|
|