support dht nodes in magnet links

This commit is contained in:
Arvid Norberg 2010-11-07 19:18:16 +00:00
parent b3c0fa9f45
commit 02e15202f2
4 changed files with 33 additions and 8 deletions

View File

@ -55,6 +55,7 @@
* added more detailed instrumentation of the disk I/O thread
* support dht nodes in magnet links
* support 100 Continue HTTP responses
* changed default choker behavior to use 8 unchoke slots (instead of being rate based)
* fixed error reporting issue in disk I/O thread

View File

@ -163,6 +163,26 @@ namespace libtorrent
return torrent_handle();
}
#ifndef TORRENT_DISABLE_DHT
std::string::size_type node_pos = std::string::npos;
std::string node = url_has_argument(uri, "dht", &node_pos);
while (!node.empty())
{
std::string::size_type divider = node.find_last_of(':');
if (divider != std::string::npos)
{
int port = atoi(node.c_str()+divider+1);
if (port != 0)
ses.add_dht_node(std::make_pair(node.substr(0, divider), port));
}
node_pos = uri.find("&dht=", node_pos);
if (node_pos == std::string::npos) break;
node_pos += 5;
node = uri.substr(node_pos, uri.find('&', node_pos) - node_pos);
}
#endif
sha1_hash info_hash;
if (btih.size() == 40 + 9) from_hex(&btih[9], 40, (char*)&info_hash[0]);
else info_hash.assign(base32decode(btih.substr(9)));

View File

@ -3678,8 +3678,7 @@ namespace aux {
void session_impl::add_dht_node_name(std::pair<std::string, int> const& node)
{
TORRENT_ASSERT(m_dht);
m_dht->add_node(node);
if (m_dht) m_dht->add_node(node);
}
void session_impl::add_dht_router(std::pair<std::string, int> const& node)
@ -3694,11 +3693,16 @@ namespace aux {
void session_impl::on_dht_router_name_lookup(error_code const& e
, tcp::resolver::iterator host)
{
if (e || host == tcp::resolver::iterator()) return;
// router nodes should be added before the DHT is started (and bootstrapped)
udp::endpoint ep(host->endpoint().address(), host->endpoint().port());
if (m_dht) m_dht->add_router_node(ep);
m_dht_router_nodes.push_back(ep);
// TODO: report errors as alerts
if (e) return;
while (host != tcp::resolver::iterator())
{
// router nodes should be added before the DHT is started (and bootstrapped)
udp::endpoint ep(host->endpoint().address(), host->endpoint().port());
if (m_dht) m_dht->add_router_node(ep);
m_dht_router_nodes.push_back(ep);
++host;
}
}
#endif

View File

@ -431,7 +431,7 @@ int test_main()
p.save_path = ".";
error_code ec;
const char* magnet_uri = "magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
"&tr=http://1&tr=http://2&tr=http://3&dn=foo";
"&tr=http://1&tr=http://2&tr=http://3&dn=foo&dht=127.0.0.1:43";
torrent_handle t = add_magnet_uri(*s, magnet_uri, p, ec);
TEST_CHECK(!ec);
if (ec) fprintf(stderr, "%s\n", ec.message().c_str());