support for receiving multi announce messages for local peer discovery

This commit is contained in:
Arvid Norberg 2011-02-16 07:41:44 +00:00
parent cb6f38f056
commit 3c05e81529
6 changed files with 43 additions and 33 deletions

View File

@ -1,3 +1,4 @@
* support for receiving multi announce messages for local peer discovery
* added session::listen_no_system_port flag to prevent libtorrent from ever binding the listen socket to port 0 * added session::listen_no_system_port flag to prevent libtorrent from ever binding the listen socket to port 0
* added option to not recheck on missing or incomplete resume data * added option to not recheck on missing or incomplete resume data
* extended stats logging with statistics=on builds * extended stats logging with statistics=on builds

View File

@ -69,7 +69,7 @@ namespace libtorrent
std::string const& header(char const* key) const std::string const& header(char const* key) const
{ {
static std::string empty; static std::string empty;
std::map<std::string, std::string>::const_iterator i std::multimap<std::string, std::string>::const_iterator i
= m_header.find(key); = m_header.find(key);
if (i == m_header.end()) return empty; if (i == m_header.end()) return empty;
return i->second; return i->second;
@ -115,7 +115,7 @@ namespace libtorrent
// reset the whole state and start over // reset the whole state and start over
void reset(); void reset();
std::map<std::string, std::string> const& headers() const { return m_header; } std::multimap<std::string, std::string> const& headers() const { return m_header; }
std::vector<std::pair<size_type, size_type> > const& chunks() const { return m_chunked_ranges; } std::vector<std::pair<size_type, size_type> > const& chunks() const { return m_chunked_ranges; }
private: private:
@ -132,7 +132,7 @@ namespace libtorrent
enum { read_status, read_header, read_body, error_state } m_state; enum { read_status, read_header, read_body, error_state } m_state;
std::map<std::string, std::string> m_header; std::multimap<std::string, std::string> m_header;
buffer::const_interval m_recv_buffer; buffer::const_interval m_recv_buffer;
int m_body_start_pos; int m_body_start_pos;

View File

@ -392,7 +392,7 @@ restart_response:
// add them to the headers in the parser // add them to the headers in the parser
for (std::map<std::string, std::string>::const_iterator i = tail_headers.begin(); for (std::map<std::string, std::string>::const_iterator i = tail_headers.begin();
i != tail_headers.end(); ++i) i != tail_headers.end(); ++i)
m_header[i->first] = i->second; m_header.insert(std::make_pair(i->first, i->second));
return true; return true;
} }

View File

@ -176,43 +176,52 @@ void lsd::on_announce(udp::endpoint const& from, char* buffer
if (port_str.empty()) if (port_str.empty())
{ {
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
m_log << time_now_string() m_log << time_now_string()
<< " <== announce: invalid BT-SEARCH, missing port" << std::endl; << " <== announce: invalid BT-SEARCH, missing port" << std::endl;
#endif #endif
return; return;
} }
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);
from_hex(ih_str.c_str(), 40, (char*)&ih[0]);
int port = std::atoi(port_str.c_str()); int port = std::atoi(port_str.c_str());
if (!ih.is_all_zeros() && port != 0) typedef std::multimap<std::string, std::string> headers_t;
headers_t const& headers = p.headers();
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)
{ {
std::string const& ih_str = i->second;
if (ih_str.size() != 40)
{
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
char msg[200]; m_log << time_now_string()
snprintf(msg, 200, "%s *** incoming local announce %s:%d ih: %s\n" << " <== announce: invalid BT-SEARCH, invalid infohash: "
, time_now_string(), print_address(from.address()).c_str() << ih_str << std::endl;
, port, ih_str.c_str());
#endif #endif
// we got an announce, pass it on through the callback continue;
#ifndef BOOST_NO_EXCEPTIONS
try {
#endif
m_callback(tcp::endpoint(from.address(), port), ih);
#ifndef BOOST_NO_EXCEPTIONS
} }
catch (std::exception&) {}
sha1_hash ih(0);
from_hex(ih_str.c_str(), 40, (char*)&ih[0]);
if (!ih.is_all_zeros() && port != 0)
{
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
char msg[200];
snprintf(msg, 200, "%s *** incoming local announce %s:%d ih: %s\n"
, time_now_string(), print_address(from.address()).c_str()
, port, ih_str.c_str());
#endif #endif
// we got an announce, pass it on through the callback
#ifndef BOOST_NO_EXCEPTIONS
try {
#endif
m_callback(tcp::endpoint(from.address(), port), ih);
#ifndef BOOST_NO_EXCEPTIONS
} catch (std::exception&) {}
#endif
}
} }
} }

View File

@ -362,8 +362,8 @@ namespace libtorrent
{ {
#ifdef TORRENT_VERBOSE_LOGGING #ifdef TORRENT_VERBOSE_LOGGING
peer_log("*** STATUS: %d %s", m_parser.status_code(), m_parser.message().c_str()); peer_log("*** STATUS: %d %s", m_parser.status_code(), m_parser.message().c_str());
std::map<std::string, std::string> const& headers = m_parser.headers(); std::multimap<std::string, std::string> const& headers = m_parser.headers();
for (std::map<std::string, std::string>::const_iterator i = headers.begin() for (std::multimap<std::string, std::string>::const_iterator i = headers.begin()
, end(headers.end()); i != end; ++i) , end(headers.end()); i != end; ++i)
peer_log(" %s: %s", i->first.c_str(), i->second.c_str()); peer_log(" %s: %s", i->first.c_str(), i->second.c_str());
#endif #endif

View File

@ -57,7 +57,7 @@ void print_http_header(http_parser const& p)
{ {
std::cerr << " < " << p.status_code() << " " << p.message() << std::endl; std::cerr << " < " << p.status_code() << " " << p.message() << std::endl;
for (std::map<std::string, std::string>::const_iterator i for (std::multimap<std::string, std::string>::const_iterator i
= p.headers().begin(), end(p.headers().end()); i != end; ++i) = p.headers().begin(), end(p.headers().end()); i != end; ++i)
{ {
std::cerr << " < " << i->first << ": " << i->second << std::endl; std::cerr << " < " << i->first << ": " << i->second << std::endl;